2017-10-18 46 views
0

我的域名是example.com沒有www。所以,如果我把www.example.com放在那裏,那麼它不起作用,但example.com的作品。所以我喜歡這個Apache永久重定向自動轉到www

<VirtualHost *:80> 
    ServerName example.com 
    ServerAdmin [email protected] 
    Redirect permanent/https://example.com/ 
</VirtualHost> 

<VirtualHost *:443> 
    ServerName example.com 
    ServerAdmin [email protected] 
    DocumentRoot path/to/project/public 
    SSLEngine on 
    SSLCertificateFile /path/to/keys/xxx.crt 
    SSLCertificateKeyFile /path/to/keys/xxx.key 
    ErrorLog /var/log/apache2/error_log 
    CustomLog /var/log/apache2/access_log combined 
    <Directory "path/to/project/public"> 
     Options FollowSymLinks 
     Require all granted 
    </Directory> 
</VirtualHost> 

配置阿帕奇現在,你可以看到,我做一個永久重定向到HTTPS像

Redirect permanent/https://example.com/ 

但是這種重定向加上www用默認的域名。所以重定向的url變成了https://www.example.com/。很明顯,我的網站無法從www訪問,因爲它是在沒有www的情況下注冊的。所以請告訴我如何使重定向工作,並在沒有https的情況下去https://example.com/

回答

0

添加一個別名

<VirtualHost *:443> 
    ServerName example.com 
    ServerAlias www.example.com 
</VirtualHost> 

這就要求www.yourdomain.com指向同一個地方yourdomain.com。但www可能不適用於您的SSL證書,它取決於證書的特殊性。

我通常在我的網站上都允許這樣做,因爲有些人在輸入地址時堅持包含www。

Apache的文檔可幫助與更多的細節https://httpd.apache.org/docs/2.2/vhosts/name-based.html

至於您所遇到的問題重定向: 確保你沒有被改寫你的非WWW請求到www一些RewriteEngine敘述規則。您的站點目錄中可能有一個.htaccess文件正在執行重寫/重定向。

它看起來是這樣的:

RewriteEngine On 
RewriteCond %{HTTPS} !=on 
RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R,L] 

這將意味着你應該刪除WWW在最後重寫規則