htaccess的重写规则:根据输入的URL重定向HTTP到HTTPS或https到http(双向) [英] htaccess rewriterule: redirect http to https or https to http (both ways) depending on URL typed

查看:136
本文介绍了htaccess的重写规则:根据输入的URL重定向HTTP到HTTPS或https到http(双向)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁请帮我写重写规则,重定向HTTP< - > HTTPS(回力取决于输入网址)这些条件:

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。

在code以下重定向所有的URL(并保持参数),除非 http://www.mydomain.com < /一>,到https。

  #redirects HTTP到HTTPS如果有参数
的RewriteCond%{} HTTPS关闭
的RewriteCond%{QUERY_STRING}!^ $
重写规则HTTPS(*)://%{HTTP_HOST}%{REQUEST_URI}
 

我试过之后,code添加下面右侧的code以上重定向HTTPS访问http以及(如果没有参数应用),这样所有的页面总是HTTPS,除了www.mydomain .COM,但我有没有运气。此外,我错过了P =回家,P =和home1,QQQ =家庭 - ???我不知道如何将它们添加

 的RewriteCond%{HTTP}关闭
的RewriteCond%{QUERY_STRING} ^ $
(。*)重写规则的http://%{HTTP_HOST}%{REQUEST_URI}
 

解决方案

尝试添加以下到您的网站的根目录下的htaccess文件。

  RewriteEngine叙述上
的RewriteBase /

#determine如果页被认为是http
#如果它具有p =家庭或P =和home1或QQQ =家在查询字符串
的RewriteCond%{QUERY_STRING}(^ |&安培;)(P =和home1 | QQQ =回家吗?)(安培; | $)[NC,OR]
#or如果查询字符串是空的
的RewriteCond%{QUERY_STRING} ^ $
#设置的环境变量,以1
重写规则^  -  [E = IS_HTTP:1]


#ALL的网页,都应该是HTTP,如果HTTPS重定向
的RewriteCond%{} HTTPS上
的RewriteCond%{ENV:IS_HTTP} 1
重写规则^ HTTP://%{HTTP_HOST}%{REQUEST_URI} [R,L = 301]

#ALL其他页面发送到HTTPS如果尚未如此
的RewriteCond%{} HTTPS关闭
的RewriteCond%{ENV:IS_HTTP} 1!
重写规则^ https://开头%{HTTP_HOST}%{REQUEST_URI} [R,L = 301]
 

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}

解决方案

Try adding the following to your htaccess file in the root directory of your site.

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]


#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

这篇关于htaccess的重写规则:根据输入的URL重定向HTTP到HTTPS或https到http(双向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆