我有一个主域https://www.domain.com
or https://domain.com
和通配符 DNS 类似https://domain.com/index.php?name=abcd
强制重定向http://abcd.domain.com
但我的登录页面还有另一个问题https://domain.com/login.php?name=abcd
我想要强有力的结果http://domain.com/login.php?name=abcd
但它无法在 HTTP 上重定向。
RewriteEngine On
#match either the www subdomain or no subdomain
RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which request index.php
RewriteCond %{REQUEST_URI} ^/index\.php
#and contain a username paramater
RewriteCond %{QUERY_STRING} ^(.*?)(?:^|&)name=([^&]+)(.*?)$
#then rewrite them to name.domain.com without the name parameter
RewriteRule ^ http://%1.domain.com%{REQUEST_URI} [R,L]
#match either the www subdomain or no subdomain
RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which was requested without https
RewriteCond %{HTTPS} off
#then redirect to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#Capture subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.(?:domain\.com)$
#If we're not working on www
RewriteCond %{HTTP_HOST} !^(?:www.domain\.com)$
#If querystring doesn't contain name
RewriteCond %{QUERY_STRING} !^(.*?)(?:^|&)name=([^&]+)(.*?)$
#Add username to querystring
RewriteRule index.php index.php?name=%1 [L,QSA]
这个 htaccess 代码工作正常,但我想在 htaccess 中登录脚本
在聊天中澄清后,这就是我需要的:
所有 URL 应重写为https
, 除了login.php
,应该停留在http
. If login.php
进来作为https
,重写为http
.
您已经有了重写规则https
,唯一剩下的就是排除login.php
RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/login\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
If login.php
被请求with https
,重写为http
RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
RewriteCond %{HTTPS} on
RewriteRule ^login.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
最后,一个小提示:never测试用301
已启用,请参阅此答案调试 .htaccess 重写规则的技巧 https://stackoverflow.com/a/9204355/1741542了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)