2017-07-17 139 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示例为你所描述的。 –