.htaccess将HTTP重定向到HTTPS,mod_rewrite不起作用

时间:2018-02-14 11:26:01

标签: .htaccess http redirect mod-rewrite https

我正在尝试使用htaccess和mod_rewrite将所有连接重定向到我的网站从http到https,我已经尝试了所有可能的事情但仍然无法正常工作,我得到一个错误:页面没有正确重定向< / p>

这是htaccess代码:

ErrorDocument 404 http://www.example.com/error.php

<IfModule mod_rewrite.c>

Options -Indexes

Options +FollowSymlinks

RewriteEngine on

RewriteBase /

RewriteCond %{HTTPS} on 

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]

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

RewriteRule ^([^/]*)\.html$ /index.php?lang=$1 [L]

RewriteRule ^sites/examplesite/([^/]*)\.html$ /index.php?lang=$1 [L]

RewriteRule ^([^/]*)/examplesite/([^/]*)\.html$ /index.php?lang=$1&page=$2 [L]

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?lang=$1&page=$2 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&idCat=$2&page=$3 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&cat=$2&filter=$3&pa=$4&page=$5 [L]

RewriteRule ^sites/examplesite/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&page=$2 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&show=$2&page=$3 [L]

RewriteRule ^sites/examplesite/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&itemID=$2&idCat=$3&page=$4 [L]

RewriteRule ^sites/examplesite/([^/]*)//([^/]*)/([^/]*)\.html$ /index.php?lang=$1&idCat=$2&page=$3 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&idCat=$2&client_id=$3&page=$4 [L]

</IfModule>

<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>



   php_flag display_errors On

1 个答案:

答案 0 :(得分:0)

关于您的主要问题,从您的代码中,我认为这部分将强制所有http请求发送到https:

RewriteCond %{HTTPS} on 

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]

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

很明显你也想为无www添加www,所以首先,当你想要捕获没有https请求时,你应该像这样:

RewriteCond %{HTTPS} off 

因为它的条件就像说,如果请求不是https但是在你的代码中你只能自己捕获https请求并再次强制它们。

使用以下代码代替上述代码:

 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R=302,L]

测试它,如果可以用302更改301以获得永久重定向