.htaccess:根据 URL 将 http 重定向到 https 或 https 到 http

发布于 2025-01-03 02:32:19 字数 1170 浏览 2 评论 0原文

下面的解决方案涵盖了必需的规则:

1) http://www.mydomain.com 、 http://www.mydomain.com/?p=home 、 http://www.mydomain.com mydomain.com/?p=home1 、 http://www.mydomain.com/?qqq=home 始终为 http,即使键入 https 而不是 http;

2) 所有其余页面始终为 https,即使键入的是 http 而不是 https;

但实际上不涵盖

3) //www.mydomain.com/administration/any_file_in_admin_folder.php 也应始终为 https(即使使用参数 ?p=home 等) )。

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=301,L]

#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=301,L]

如何实现 3) 这段代码才能正常工作? (我仍然没有运气,需要帮助)。 谢谢。

The solution below covers required rules:

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;

but in practice does NOT cover

3) //www.mydomain.com/administration/any_file_in_admin_folder.php should always be https as well (even with parameter ?p=home etc).

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=301,L]

#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=301,L]

How to implement 3) to this code so it works? (I still have no luck and need help).
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

帥小哥 2025-01-10 02:32:19

试试这个:
在第二行和第二行中添加第一行第三块。

RewriteCond %{REQUEST_URI} !/administration/ [NC]

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#Requested URi must not have administration in it
RewriteCond %{REQUEST_URI} !/administration/ [NC]
#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]  [L]

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

#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=301,L]

Try this:
Add the first line in 2nd & 3rd block.

i.e. RewriteCond %{REQUEST_URI} !/administration/ [NC]

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#Requested URi must not have administration in it
RewriteCond %{REQUEST_URI} !/administration/ [NC]
#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]  [L]

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

#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=301,L]
空心空情空意 2025-01-10 02:32:19

将您的第一条规则更改为:

#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} ^$
#or if request URI is: /administration/any_file_in_admin_folder\.php
RewriteCond %{REQUEST_URI} !^/*administration/any_file_in_admin_folder\.php$ [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

Change your first rule to:

#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} ^$
#or if request URI is: /administration/any_file_in_admin_folder\.php
RewriteCond %{REQUEST_URI} !^/*administration/any_file_in_admin_folder\.php$ [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
罗罗贝儿 2025-01-10 02:32:19

规则的第二部分 RewriteCond %{QUERY_STRING} ^$ 匹配任何不带查询字符串的 URL。您的网址 //www.mydomain.com/administration/any_file_in_admin_folder.php 没有查询字符串。因此,IS_HTTP 设置为 1,并且您的用户将被重定向到 HTTP。

试试这个。它未经测试 - 但基本上您首先识别“home”查询字符串,然后单独处理 http://www.mydomain.com

#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteRule ^$ - [E=IS_HTTP:1]

这也可能会起作用:

#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 %{REQUEST_URI} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

The second part of your rule, RewriteCond %{QUERY_STRING} ^$ matches any URL without a querystring. Your URL, //www.mydomain.com/administration/any_file_in_admin_folder.php has no querystring. So IS_HTTP is being set to 1, and your user is being redirected to HTTP.

Try this. It's untested - but basically you're identifying the "home" querystring first, and then handling http://www.mydomain.com separately.

#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteRule ^$ - [E=IS_HTTP:1]

This might also do the trick:

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