【问题标题】:incorrect redirect from http to https从 http 到 https 的错误重定向
【发布时间】:2017-04-09 03:17:51
【问题描述】:

尝试使用 https,但是当我在 .htaccess 中使用以下内容时,出现重定向问题错误

这是我的.htaccess中的内容

RewriteEngine On
 RewriteBase /

 RewriteCond %{HTTP} on [OR]
 RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

 #Uncomment lines below when uploading to the live server
 RewriteCond %{HTTP_HOST} !^(www\.)example\.com$ [NC]
 RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-l
 RewriteRule ^.*$ index.php [QSA,L,NC]

 #Uncomment lines below when uploading to the live server
 RewriteCond %{HTTP_REFERER} !^$
 RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com/.*$ [NC]
 RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]

请帮忙

【问题讨论】:

  • 如果可以,请考虑定义重定向的方法,如果可以避免使用 .htaccess,并且在 virtualhsot 中定义大部分内容会更好。大多数时候重定向到 ssl 涉及定义适当的虚拟主机和一个简单的重定向/hostname,request_filename 检查然后转到 index.php,都可以为单个 Fallbackresource 指令删除,等等...

标签: apache .htaccess mod-rewrite https http-redirect


【解决方案1】:

您的第一条规则有许多错误的[OR] 条件,这会导致重定向循环。

将您的第一条规则更改为:

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

并在清除浏览器缓存后进行测试。

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 2017-10-06
    • 2014-10-08
    • 2014-12-20
    • 2017-11-04
    • 2016-01-17
    • 2011-09-28
    • 2017-07-18
    • 2018-04-09
    相关资源
    最近更新 更多