【发布时间】:2017-07-24 15:52:44
【问题描述】:
我需要将所有 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
我在 Google 上搜索过几次并遇到过这个解决方案:
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]
当我加载页面时,它给了我一个不正确的重定向错误。请告诉我我做错了什么。
【问题讨论】:
-
您的 SSL 是如何管理的?您是否在应用程序服务器上直接安装了 SSL 证书? “不正确的重定向错误”是什么意思?浏览器是否报告了重定向循环?
-
或者.... 检查
$_SERVER数组的转储并检查HTTPS和X-Forwarded-Proto的值(可能还有其他一些特定于服务器的X-....标头?)。 -
因为我们需要找出不起作用的原因以及用于服务器配置的正确指令。您发布的重定向代码没有任何“错误” - 该代码在大多数安装了 SSL 证书的站点/服务器上都可以正常工作(这就是为什么您“多次”找到相同的代码 - 它可以工作)。但由于某种原因,它无法在您的站点/服务器上运行 - 这是“好的” - 该代码无法在 every 服务器配置上运行。但通常有一个很好的理由让它不起作用 - 这就是为什么我要专门询问如何 SSL证书是实施的,而不是“SSL是否有效”。
-
重定向循环最可能的原因是
HTTPS服务器变量未设置,或未按预期设置。如果是这种情况,那么我们需要找到另一个要使用的服务器变量。我会将SERVER_PORT添加到要检查的$_SERVER变量列表中。 -
哦!从来不知道,对不起。实际上 $_SERVER['HTTPS'] 总是空的,但是当我在 https 页面上时,$_SERVER['HTTP_X_FORWARDED_PROTO'] 返回 https
标签: apache .htaccess redirect mod-rewrite https