子域名重定向至主域名

子域名重定向至主域名

在创建包含 apache 的新 vps 服务器时,我添加了一个域和一个子域,但我对子域将我重定向到主域的问题感到困惑,我认为问题来自 .htaccess 文件:

  • www.example.com
  • www.two.example.com

  • 第一个位于 /var/www/example.com/ 包含 wordpress cms。

  • 第二个位于 /var/www/two.example.com/仅包含index.html。

当然,我在 Web 应用程序防火墙中添加了一个新的条目 A

two.example.com A <ip_of_vps>

www.two.example.com A <ip_of_vps>

并且两个站点都启用了 a2ensite

$ ls -all /var/apache/sites-enabled
lrwxrwxrwx 1 root root   33 Mar  5 22:41 example.com.conf -> ../sites-available/example.com.conf
lrwxrwxrwx 1 root root   50 Mar 22 01:16 example.com-le-ssl.conf -> /etc/apache2/sites-available/example.com-le-ssl.conf
lrwxrwxrwx 1 root root   37 Apr 20 03:42 two.example.com.conf -> ../sites-available/two.example.com.conf

当我 ping two.example.com 时一切正常

apachectl -S 的结果

VirtualHost configuration:
*:443                  is a NameVirtualHost 
default server example.com (/etc/apache2/sites-enabled/example.com- le-ssl.conf:2)
         port 443 namevhost example.com (/etc/apache2/sites-enabled/example.com-le-ssl.conf:2)  alias www.example.com

*:80                   is a NameVirtualHost
         default server example.com (/etc/apache2/sites-enabled/example.com.conf:2)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:2) alias www.example.com
         port 80 namevhost two.example.com (/etc/apache2/sites-enabled/two.example.com.conf:1) alias www.two.example.com

/etc/apache2/sites-available/ 的配置

对于 www.example.com

<VirtualHost *:80>   
     ServerAlias www.example.com
     ServerName example.com
     ServerAdmin [email protected]
     DocumentRoot /var/www/example.com/

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined

     RewriteEngine on
     RewriteCond %{SERVER_NAME} =www.example.com [OR]
     RewriteCond %{SERVER_NAME} =example.com
     RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

对于 www.two.example.com

<VirtualHost *:80>

        ServerName two.example.com
        ServerAlias www.two.example.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/two.example.com/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SecRuleEngine On

</VirtualHost>

以及 www.example.com (wordpress) 的 .htaccess :

# BEGIN WordPress
# Les directives (lignes) entre 'BEGIN WordPress' et 'END WordPress' sont
# généré dynamiquement, et ne doivent uniquement être modifiées via les filtres WordPress.
# Toute modification des directives entre ces marqueurs sera outrepassée.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^adm/?$ /wp-login.php [QSA,L]
RewriteRule ^adm/register/?$ /wp-login.php?action=register [QSA,L]
RewriteRule ^adm/lostpassword/?$ /wp-login.php?action=lostpassword [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]
RedirectMatch 404 /\.git.*
</IfModule>

# END WordPress

相关内容