URL在Apache HTTPS中被破坏

URL在Apache HTTPS中被破坏

问题描述:

我遇到一个问题,即当站点在https中时,我站点中的所有链接都被破坏。无论如何,我需要做的是将所有请求从http重定向到https,所以我添加了下面的代码.htaccessURL在Apache HTTPS中被破坏

# Redirect from http to https. 
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^project-local\.com* 
RewriteRule ^(.*)$ https://project-local.com/$1 [L,R=301] 

重定向工作正常,但如果我点击任何链接在该网站,而它在所有的HTTPS响应是发生什么事404

这是我的SSL虚拟主机的样子:

<IfModule mod_ssl.c> 
    <VirtualHost _default_:443> 
     ServerName project-local.com 
     DocumentRoot /home/snake/repo/project-a/docroot 

     ErrorLog ${APACHE_LOG_DIR}/project-a-ssl_error.log 
     CustomLog ${APACHE_LOG_DIR}/project-a-ssl_access.log common 
     SSLEngine on 

     SSLCertificateFile /etc/apache2/ssl/apache.crt 
     SSLCertificateKeyFile /etc/apache2/ssl/apache.key 

     <FilesMatch "\.(cgi|shtml|phtml|php)$"> 
      SSLOptions +StdEnvVars 
     </FilesMatch> 
     <Directory /usr/lib/cgi-bin> 
      SSLOptions +StdEnvVars 
     </Directory> 

     <Directory /home/snake/repo/project-a/docroot/> 
      Require all granted 
     </Directory> 

     BrowserMatch "MSIE [2-6]" \ 
             nokeepalive ssl-unclean-shutdown \ 
             downgrade-1.0 force-response-1.0 
     # MSIE 7 and newer should be able to use keepalive 
     BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown 
    </VirtualHost> 
</IfModule> 

http->https规则是不正确的,它可以导致无限重定向,您可以使用:

RewriteCond %{HTTP_HOST} ^project-local\.com$ [NC] 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE] 
+0

我修改了基于你的答案的规则,但仍然有404. – ninjascorner

+0

哦,另一件事是网站布局也被打破。 CSS文件丢失。 – ninjascorner

+0

@anubhava ...例如,我看到“https:// project-local.com/about-us”。 – ninjascorner