Dot-Htaccess

同一伺服器上的兩個站點具有不需要的重定向

  • May 26, 2020

我有兩個不同的站點

domain1.com

domain2.com

問題:我只能使用 www.domain2.com 訪問 domain2.com

如果我嘗試使用 domain2.com,它會重定向到 domain1.com。

配置文件:

domain1.com.conf

  <VirtualHost *:80>     
     ServerName domain1.com
     ServerAlias www.domain1.com
     DocumentRoot /var/www/html
   </VirtualHost>
   <IfModule mod_ssl.c>
     <VirtualHost *:443>
       DocumentRoot /var/www/html
       ServerName www.domain1.com
       Include /etc/letsencrypt/options-ssl-apache.conf
       SSLCertificateFile /etc/letsencrypt/live/domain1.com/fullchain.pem
       SSLCertificateKeyFile /etc/letsencrypt/live/domain1.com/privkey.pem
     </VirtualHost>
   </IfModule>

domain2.com.conf

   <VirtualHost *:80>
           ServerName domain2.com
           ServerAlias www.domain2.com
           DocumentRoot /var/www/public_html/domain2.com/wordpress
   </VirtualHost>
   <IfModule mod_ssl.c>
   <VirtualHost *:443>
           ServerAdmin gneri94@gmail.com
           DocumentRoot /var/www/public_html/domain2.com/wordpress
           ServerName www.domain2.com
           Include /etc/letsencrypt/options-ssl-apache.conf
           SSLCertificateFile /etc/letsencrypt/live/domain2.com/fullchain.pem
           SSLCertificateKeyFile /etc/letsencrypt/live/domain2.com/privkey.pem
   </VirtualHost>
   </IfModule>

.htaccess 在兩個站點

# BEGIN WordPress                         
 RewriteEngine On 
 RewriteBase / 
 RewriteRule ^index\.php$ - [L] 
 RewriteCond %{REQUEST_FILENAME} !-f 
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteRule . /index.php [L] 

命令certbot certificates返回:

證書名稱:domain1.com

域:domain1.com www.domain1.com

證書路徑:/etc/letsencrypt/live/domain1.com/fullchain.pem

私鑰路徑:/etc/letsencrypt/live/domain1.com/privkey。佩姆

證書名稱:domain2.com

域:domain2.com www.domain2.com

證書路徑:/etc/letsencrypt/live/domain2.com/fullchain.pem

私鑰路徑:/etc/letsencrypt/live/domain2.com/privkey。佩姆

對於埠 80 vHost,您有:

ServerName domain2.com
ServerAlias www.domain2.com

但是對於埠 443,您只有:

ServerName www.domain2.com

(這適用於 domain1 和 domain2。)

因此,如果您訪問https://domain2.com它將被預設 vHost 擷取(即第一個定義的www.domain1.com:)

您還應該考慮重定向到埠 80 vHost 中的 HTTPS(規範)域。

引用自:https://serverfault.com/questions/1018692