Php 将所有www或非www页面重定向到https://www 除了一页

Php 将所有www或非www页面重定向到https://www 除了一页,php,url-redirection,Php,Url Redirection,我正在努力执行https://www.在我所有的页面上,除了hotels.php。 下面我的当前代码添加了两次www.,即https://www.www.example.com .htaccess RewriteEngine On RewriteCond %{HTTPS} =off RewriteCond %{REQUEST_URI} !^\/hotels.php\/ RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301] Rewr

我正在努力执行
https://www.
在我所有的页面上,除了
hotels.php
。 下面我的当前代码添加了两次
www.
,即
https://www.www.example.com

.htaccess

RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]    

RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/hotels.php\/
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
让我知道我在这里遗漏了什么导致了问题。
感谢您不是Htaccess正则表达式的主控程序,这对我很有用:

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
    RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>

重写cond%{HTTPS}关闭[或]
重写cond%{HTTP_HOST}^www\。[北卡罗来纳州]
重写条件%{HTTP_HOST}^(.*)$[NC]
重写规则(.*)https://www.%1/$1[R=301,L]

希望有帮助!StackOverflow上有很多答案。。。我很确定这可能是我从哪里得到的

尝试以下重写条件:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force https:// for all except your desired URLs    
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/hotels.php/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force http:// for your desired URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /hotels.php/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

在检查HTTPS时,必须检查请求URI中是否包含www。试试这个:

//If has not www and it's not a subdomain adds www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

//If has not https and it's not hotels.php add https
RewriteCond %{REQUEST_SCHEME} !https
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

已在Stackoverflow中被询问和回答。@jasie请投票关闭/标记为重复,并在.htaccess“循环”中配置适当的Q/A。重写,直到不再有规则匹配为止。在您从外部重定向到
https://www.example.com/foo
已经(或者如果您要求这样做),您的第一组条件和规则将再次重定向-但是
HTTP\u HOST
变量现在已经包含
www.example.com
,您可以在前面添加另一个
www.
。您需要在该位置设置一个附加条件,检查主机名是否已以
www.
开头,请参见示例。所有可能的重复项都可以正常使用,但hotels.php仍使用https://加载,而不是将其重定向到http://