http到https重定向通过htaccess:错误的重定向错误

时间:2017-07-24 15:52:44

标签: .htaccess http https

我需要所有http来重定向https:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^some-domain\.com$ [NC]
RewriteRule ^(.*)$ https://some-domain.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} /(\.+) [OR]
RewriteCond %{THE_REQUEST} /(\?+) [OR]
RewriteCond %{THE_REQUEST} /(/+)
RewriteRule ^(.*)$ 404.html [L]
RewriteRule ^core/(install|temp|smarty|modules|languages|includes|functions|fonts|files|config|classes|cache|backup|tpl)/(.*) - [F]
RewriteRule ^data/(.+)\.(tpl\.html|php|php3|php4|php5|phtml|pl|cgi) - [F]
RewriteRule ^install_check\.html$ install.php?check=yes [L]
RewriteRule ^index\.html$ index.php [L]
RewriteRule ^news\.html$ index.php?news=yes [L]
RewriteRule ^price\.html$ index.php?show_price=yes [L]
RewriteRule ^cart\.html$ index.php?shopping_cart=yes [L]
RewriteRule ^wide_search\.html$ index.php?search_with_change_category_ability=yes [L]
RewriteRule ^feedback\.html$ index.php?feedback=yes [L]
RewriteRule ^compare\.html$ index.php?comparison_products=yes [L]
RewriteRule ^page_([0-9]+)\.html$ index.php?show_aux_page=$1 [L]
RewriteRule ^product_([0-9]+)\.html$ index.php?productID=$1 [L]
RewriteRule ^category_([0-9]+)\.html$ index.php?categoryID=$1 [L]
RewriteRule ^category_([0-9]+)_offset_([0-9]+)\.html$ index.php?categoryID=$1&offset=$2 [L]
RewriteRule ^category_([0-9]+)_show_all\.html$ index.php?categoryID=$1&show_all=yes [L]
RewriteRule ^show_news_([0-9]+)\.html$ index.php?fullnews=$1 [L]

# BEGIN Articles
RewriteRule ^poleznoe/([^/]+)\.html$ index.php?fullarticles=$1 [L]
RewriteRule ^poleznoe/([0-9]+)/$ index.php?articles=yes&offset=$1 [L]
RewriteRule ^poleznoe/$ index.php?articles=yes [L]
# END Articles

RewriteRule ^google([a-z0-9_-]+).html$ google$1.html [L]
RewriteRule ^yandex([a-z0-9_-]+).html$ yandex$1.html [L]

# BEGIN Human friendly URL's
RewriteRule ^news/([^/]*).html$ index.php?uri=$1&uriFor=news [L]
RewriteRule ^([^/]*).html$ index.php?uri=$1&uriFor=pages [L]
RewriteRule ^([^/]*)/$ index.php?uri=$1&uriFor=category [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !([^/]*)/$
RewriteRule ^([^/]*)$  $1/ [L,R=301]
RewriteRule ^(([^/]*)_offset_([0-9]+))/$ index.php?uri=$1&uriFor=category&offset=$2 [L]
RewriteRule ^([^/]*)_show_all/$ index.php?uri=$1&uriFor=category&show_all=yes [L]
#RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ index.php?uri=$3&uriFor=product [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ index.php?uri=$2&uriFor=product [L]
# END Human friendly URL's

我用谷歌搜索了几次这个解决方案:

RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但如果我将其放在RewriteEngine On之后或RewriteBase /之后或其他任何地方或之间

RewriteCond %{HTTP_HOST} !^some-domain\.com$ [NC]
RewriteRule ^(.*)$ https://some-domain.com/$1 [R=301,L]

加载页面时,它给出了错误的重定向错误。请告诉我我做错了什么。

提前致谢。

1 个答案:

答案 0 :(得分:3)

  

$_SERVER['HTTPS']始终为空,但当我在https页面时,$_SERVER['HTTP_X_FORWARDED_PROTO']会返回https

这告诉您,您的SSL由前端代理服务器管理,而不是您的应用程序服务器。您的应用程序服务器似乎没有安装SSL证书,并且最有可能通过端口80向代理服务器提供普通的HTTP请求。然后,代理服务器向客户端提供安全的HTTPS响应,因此客户端请求是安全的。

这意味着,如果您的应用程序服务器尝试通过简单地检查标准服务器变量(即HTTPSSERVER_PORT)将HTTP重定向到HTTPS,那么这是可能因重定向循环而失败,因为HTTPS总是关闭SERVER_PORT始终为80 - 即使在重定向之后也是如此。 (请注意,HTTPS Apache变量很可能是" off",但这通常转​​换为为空的PHP超全局$_SERVER['HTTPS'] - &# 39; s就是PHP如何处理它。)

请注意,HTTP到HTTPS重定向可以在前端代理中执行,而不是在它到达应用程序服务器之前。 (Cloudflare使用页面规则执行此操作。)

或者,您可以检查代理服务器设置的其他HTTP请求标头,因为请求转发从代理到您的应用程序服务器。 X-Forwarded-Proto标头(由PHP存储为$_SERVER['HTTP_X_FORWARDED_PROTO'])由代理服务器设置,并告诉您客户端用于连接代理的协议。

所以,将规范的重定向结合在一起:

# www to non-www redirect
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) https://example.com/$1 [R=301,L]

# http to https redirect
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

使用您发布的指令的格式,www至非www重定向应首先出现,以避免在请求http://www.example.com(即HTTP和www)时出现双重重定向。

请注意,当您处于处理SSL的代理服务器后面时,您应该仅使用此类型的HTTP进行HTTPS重定向。否则,X-Forwarded-Proto HTTP请求标头可能被恶意客户端请求伪造(但是,这应该由您的代理服务器处理)。

如果您愿意,可以将这两个规则合并为一条规则:

# Canonical redirect (no-www and HTTPS)
RewriteCond %{HTTP_HOST} !^example\.com [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://example.com/$1 [R=301,L]
相关问题