首页 > 解决方案 > 重定向到 https 条件

问题描述

如果没有“mob”后缀,我想将我的用户重定向到 https。

例子:

如果 url = http://example.com/(anything)/mob不要重定向到 https

如果 url = http://example.com/(anything) 重定向到 https

如果 url = http://example.com 重定向到 https

我尝试了以下代码但没有用。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/(.*)/mob$
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

标签: apache.htaccess

解决方案


THE_REQUEST用变量试试这个规则:

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} !/mob[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

确保在新浏览器中测试或清除浏览器缓存以避免旧缓存。

建议您将此规则作为您的首要规则。


推荐阅读