htaccess 重写规则:根据输入的 URL 将 http 重定向到 https 或 https 到 http(两种方式)
任何人都可以帮我编写重写规则,在以下条件下重定向 http<->https (返回并强制取决于键入的 URL):
1) http://www.mydomain.com 、 http://www .mydomain.com/?p=home 、 http://www.mydomain.com/?p=home1 、 http://www.mydomain.com/?qqq=home
始终为 http,即使 https键入而不是 http。
2) 所有其余页面始终为 https,即使键入的是 http 而不是 https。
下面的代码将除 http://www.mydomain.com 之外的所有网址(并保留参数)重定向到 https 。
#redirects http to https if there are parameters
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !^$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
我尝试在上面的代码之后添加下面的代码,以将 https 重定向到 http(如果没有参数),以便所有页面始终都是 https,除了 www.mydomain.com,但我没有运气。我还错过了 ?p=home、?p=home1、?qqq=home - 我不知道如何添加它们。
RewriteCond %{HTTP} off
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
Could anybody please help me to write rewriterule, that redirects http<->https (back and force depending of URL typed) with these conditions:
1) http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home
are always http, even if https are typed instead of http.
2) all the rest pages are always https, even if http was typed instead of https.
The code below redirects all urls (and keeps parameters), except http://www.mydomain.com , to https.
#redirects http to https if there are parameters
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !^$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I've tried to add the code below right after the code above to redirect https to http as well (if there are no parametres), so that all pages are always https, except www.mydomain.com, but I had no luck. Also I missed ?p=home, ?p=home1, ?qqq=home - I don't know how to add them.
RewriteCond %{HTTP} off
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将以下内容添加到站点根目录中的 htaccess 文件中。
Try adding the following to your htaccess file in the root directory of your site.