htaccess 重写规则:根据输入的 URL 将 http 重定向到 https 或 https 到 http(两种方式)

发布于 2024-12-27 21:07:56 字数 885 浏览 1 评论 0原文

任何人都可以帮我编写重写规则,在以下条件下重定向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2025-01-03 21:07:56

尝试将以下内容添加到站点根目录中的 htaccess 文件中。

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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文