的.htaccess HTTPS重定向到http除了一个页面以及WWW到非www重定向、页面、htaccess、HTTPS

2023-09-02 00:56:52 作者:时光小偷/*

在我使用htaccess的我的域名之一

on one of my domain i am using htaccess

RewriteCond %{SERVER_PORT} ^443$  [OR]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L] 

这重定向所有的https到http以及WWW非www的域名。

Which redirect all https to http as well as www to non-www domain.

现在,我想secure.php页使用HTTPS协议而不是HTTP协议,还与WWW到非www重定向。

Now, i want secure.php page to use https protocol instead of http protocol also with redirection of www to non-www.

我可以用下面的线达致这...

i can use below lines to acheive this...

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} /secure.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !/secure.php [NC]
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L] 

在我的第一个htaccess的,如果用户访问 https://www.example.com 然后重定向到 http://example.com 单重定向。

in my first htaccess if user access https://www.example.com then in redirects to http://example.com in single redirect.

后修改,以确保secure.php页,在第二htaccess的考虑情况下,如果用户访问 HTTP:// WWW .example.com的/ secure.php - >重定向到 http://example.com/secure.php - >重定向到 https://example.com/secure.php 在这里,我得到两个重定向。

after modification to secure secure.php page, on second htaccess consider case if user access http://www.example.com/secure.php ->redirects to http://example.com/secure.php -> redirect to https://example.com/secure.php here i get two redirects.

PLS建议我怎么能俱乐部我的标准,就像在我的第一个htaccess的使用[OR],所以不是在所有情况下,它使用atmost 1重定向到达最后一页。

Pls suggest how can i club my criteria like in my first htaccess using [OR], so than in all cases it uses atmost one redirect to reach final page.

推荐答案

尝试:

RewriteCond %{REQUEST_URI} !^/secure.php
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L] 

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(secure.php.*) https://example.com/$1 [R=301,L]