2017-07-17 129 views
0

我們在我們的網絡服務器上有兩個conf。http://example.com不會重定向到https://www.example.com

HTTP:

<VirtualHost *:80> 
    ServerName www.example.de 
    ServerAlias example.de www.example.at example.at www.example.net example.net 

    RewriteEngine On 
    RewriteCond %{HTTP_HOST} off 
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} 
    # Error page is just the index telling about the situation of not being connected 
    ErrorDocument 404 /index.html 
</VirtualHost> 

的https:

<IfModule mod_ssl.c> 
NameVirtualHost *:443 
<VirtualHost *:443> 
    ServerName www.example.de 
    ServerAlias example.de www.example.at example.at www.example.net example.net 

    SSLEngine On 
    SSLCertificateFile /etc/letsencrypt/live/www.example.de/fullchain.pem 
    SSLCertificateKeyFile /etc/letsencrypt/live/www.example.de/privkey.pem 

    RewriteEngine On 
    RewriteCond %{HTTP_HOST} !^www\. [NC] 
    RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}$1 [R=301,L] 

    DocumentRoot /var/www/homepage/web 
    <Directory /var/www/homepage/web> 
      AllowOverride All 
      Order Allow,Deny 
      Allow from All 
    </Directory> 



      ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
    <Directory "/usr/lib/cgi-bin"> 
      AllowOverride None 
      Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
      Order allow,deny 
      Allow from all 
    </Directory> 

    ErrorLog ${APACHE_LOG_DIR}/error.log 

    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
LogLevel warn 

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

</VirtualHost> 
</IfModule> 

當我嘗試打開http://www.example.com然後它會被重定向到https://www.example.com

當我嘗試打開http://example.comhttps://example.com,然後我出現「頁面不可用」的錯誤

我的兩個Web服務器上的conf文件有什麼問題? 兩者都應該重定向到https://www.example.com網址。

+0

不,HTTP_HOST不會包含'off'值...除非您嘗試訪問'http:// off' – CBroe

+0

我應該在哪裏添加這個可以將其設置爲關閉的HTTP_HOST? –

+0

爲什麼不配置apache虛擬主機沒有重定向,並從.htaccess文件進行重定向? –

回答

1

請儘量避免mod_rewrite,除非沒有其他選項。正如你所看到的那樣,這很複雜,並且會讓你的生活更加艱難。

進行單一的重定向將盡一切重定向到SSL虛擬主機:

Redirect/https://www.example.de/ 

注:重定向取決於mod_alias中,所以沒有必要在這樣或無論是在非SSL虛擬主機RewriteEngine敘述。


如果你堅持使用mod_rewrite,因爲你有很多名字和需要的變量:

RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [R,L] 
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L] 

第一個抓住這些情況下,主機不以www開始,第二捕捉休息。

+0

對不起,但這並不能解決問題,http://example.com和https://example.com會重定向到https://www.example.com。這也可以看到「網站不可用,子域名可用,如https://app.example.com或https://partner.example.com。這個子域名將在服務器上有另一個目錄。 –

+1

@MiracleJohnson增加了mod_rewrite示例爲你所描述的。 –