通过htaccess将HTTP重定向到https:重定向错误不正确

Tim*_*rov 2 .htaccess https http

我需要所有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
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索并多次遇到这种解决方案:

RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

但是如果我将其放置在之后RewriteEngine On或之后RewriteBase /或附近或之间的任何位置

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

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

提前致谢。

MrW*_*ite 5

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

这告诉您SSL是由前端代理服务器而非应用程序服务器管理的。您的应用程序服务器似乎未安装SSL证书,并且很可能正在通过端口80向代理服务器提供纯HTTP请求。然后,代理服务器将安全的HTTPS响应发送回客户端,因此客户端的请求是安全的。

这意味着如果您的应用程序服务器仅通过检查标准服务器变量(即HTTPSSERVER_PORT)尝试将HTTP重定向到HTTPS,则此操作很可能会因重定向循环而失败,因为HTTPS它始终处于关闭状态SERVER_PORT并且始终为80(即使在重定向之后) 。(请注意,HTTPSApache变量可能为“ off”,但这通常会转换$_SERVER['HTTPS']的PHP超全局变量-这就是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]
Run Code Online (Sandbox Code Playgroud)

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

请注意,只有在处理SSL的代理服务器之后,才应使用这种类型的HTTP到HTTPS重定向。否则,X-Forwarded-Proto恶意的客户端请求可能会伪造HTTP请求标头(但是,应由您的代理服务器处理)。


如果您愿意,可以将这两个规则组合为1个规则:

# 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]
Run Code Online (Sandbox Code Playgroud)