htaccess的重写规则强制重定向到HTTPS的HTTP [英] .htaccess rewrite rule for forcefully redirect https to http
问题描述
我有一个主域 https://www.domain.com
或 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。
I have a primary domain https://www.domain.com
or https://domain.com
and a wildcard DNS like https://domain.com/index.php?name=abcd
redirect on forcefully http://abcd.domain.com
but I have another problem my login page is https://domain.com/login.php?name=abcd
and I want result like forcefully http://domain.com/login.php?name=abcd
but it cannot redirect on 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的code工作正常,但我想登录htaccess的脚本
this htaccess code is working fine but i want login script in htaccess
澄清聊天后,这是我所需要的:
After clarification in chat, this is what I need:
所有的URL应该重写 HTTPS
,除了的login.php
,它应该呆在 HTTP
。如果的login.php
的当属 HTTPS
,把它改写为 HTTP
。
All URLs should be rewritten to https
, except login.php
, which should stay at http
. If login.php
comes in as https
, rewrite it to http
.
推荐答案
您已经有重写 HTTPS
规则,唯一剩下的是不包括的login.php
You already have the rule for rewriting to https
, the only thing left is excluding 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]
如果的login.php
请求的与的 HTTPS
,把它改写为 HTTP
If login.php
is requested with https
, rewrite it to http
RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
RewriteCond %{HTTPS} on
RewriteRule ^login.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
最后,一个小提示:从来没有的测试与 301
启用,看到这个答案的提示调试的.htaccess重写规则了解详细信息。
Finally, a minor hint: never test with 301
enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
这篇关于htaccess的重写规则强制重定向到HTTPS的HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!