Apache SSL 重定向循环

Apache SSL 重定向循环

我们正在尝试将网站从 http 重定向到 https。我们在 .htaccess 中尝试过的所有方法都导致了重定向循环。如果我们在 url 前面手动输入“https://”,我们的页面将返回jQuery error!。所以我怀疑这与并非所有请求的资源都是通过 https 请求的有关?

我们的 htaccess 文件:

RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

# change rewrite base if not in root
RewriteBase /

# api
RewriteRule ^(api)($|/) - [L]

# index.php
RewriteRule ^index.php$ - [L]
# pass-through
RewriteRule ^(css|js|site)/.*$ - [L]

# redirect errors
ErrorDocument 400 /error/400/
ErrorDocument 401 /error/401/
ErrorDocument 403 /error/403/
ErrorDocument 404 /error/404/
ErrorDocument 500 /error/500/
RewriteRule ^error/(.*)/$ index.php?page=error&eid=$1 [L]

# login
RewriteRule ^login$ login/ [R]
RewriteRule ^login/$ index.php?page=login
# logout
RewriteRule ^logout$ logout/ [R]
RewriteRule ^logout/$ index.php?page=logout
# ip requests
RewriteRule ^request_ip$ request_ip/ [R]
RewriteRule ^request_ip/$ index.php?page=request_ip
# IE fix
RewriteRule ^login/dashboard/$ dashboard/ [R]
RewriteRule ^logout/dashboard/$ dashboard/ [R]

# upgrade
RewriteRule ^upgrade$ upgrade/ [R]
RewriteRule ^upgrade/$ index.php?page=upgrade
# install
RewriteRule ^install$ install/ [R]
RewriteRule ^install/$ index.php?page=install

# dashboard
RewriteRule ^dashboard/$ index.php?page=dashboard

# widgets
RewriteRule ^widgets/(.*)/$ index.php?page=widgets&subpage=$1

# subnets
RewriteRule ^subnets/(.*)/(.*)/ipdetails/(.*)/$ index.php?page=subnets&section=$1&subnetId=$2&ipaddrid=$3 [L]
RewriteRule ^subnets/(.*)/(.*)/changelog/$ index.php?page=subnets&section=$1&subnetId=$2&sPage=changelog [L]
RewriteRule ^subnets/(.*)/(.*)/(.*)/$ index.php?page=subnets&section=$1&subnetId=$2&sPage=$3 [L]
RewriteRule ^subnets/(.*)/(.*)/$ index.php?page=subnets&section=$1&subnetId=$2 [L]
RewriteRule ^subnets/(.*)/$ index.php?page=subnets&section=$1 [L]

# folders
RewriteRule ^folder/(.*)/(.*)/$ index.php?page=folder&section=$1&subnetId=$2 [L]
RewriteRule ^folder/(.*)/$ index.php?page=folder&section=$1 [L]

# vlans
RewriteRule ^vlan/(.*)/(.*)/$ index.php?page=vlan&section=$1&vlanId=$2 [L]

# vrfs
RewriteRule ^vrf/(.*)/(.*)/$ index.php?page=vrf&section=$1&vrfId=$2 [L]

# changelog override
RewriteRule ^tools/changelog/(.*)/(.*)/$ index.php?page=tools&toolsId=changelog&cfilter=$1&climit=$2 [L]
RewriteRule ^tools/changelog/(.*)/$ index.php?page=tools&toolsId=changelog&climit=$1 [L]
# search override
RewriteRule ^tools/search/(.*)$ index.php?page=tools&toolsId=search&ip=$1 [L]
# devices override
RewriteRule ^tools/devices/hosts/(.*)$ index.php?page=tools&toolsId=devices&deviceid=$1 [L]
# tools
RewriteRule ^tools/(.*)/$ index.php?page=tools&toolsId=$1 [L]
RewriteRule ^tools/$ index.php?page=tools&toolsId=showAll [L]

# admin
RewriteRule ^administration/manageSection/sectionChangelog/(.*)/$ index.php?page=administration&adminId=sectionChangelog&sectionId=$1 [L]
RewriteRule ^administration/(.*)/$ index.php?page=administration&adminId=$1 [L]
RewriteRule ^administration/$ index.php?page=administration&adminId=showAll [L]

手动将 https 放入 url 中(不修改 htaccess)会产生以下结果:

在此处输入图片描述

它看起来应该像这样:

在此处输入图片描述

答案1

我这样做了:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]

而且它运行良好。

相关内容