我正在尝试将一个特定网址,例如/ admin,从我的域重定向到http,而其他人都应该重定向到https。例如:
Works:
http://example.com -> https://example.com
http://example.com/with-url -> https://example.com/with-url
https://example.com/admin -> http://example.com/admin
Does not work:
What I get (redirects from https to http, if under /admin):
https://example.com/admin/anything-below-here -> http://example.com/admin/anything-below-here
What I want (to stay on https):
https://example.com/admin/anything-below-here -> https://example.com/admin/anything-below-here
这是我到目前为止所得到的:
RewriteEngine On
# force https:// for all except /admin
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/admin [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for /admin URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /admin [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# all the others
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
有谁能告诉我这里缺少什么?非常感谢!
答案 0 :(得分:0)
您的正则表达式是贪婪的,因此/admin/something
与/admin
匹配,因此在admin
内重定向从https到http。
尝试使用/admin[/]*$
代替/admin
而/admin/
应重定向到http,而不是/admin/something
为了检查实际请求URI是否在admin
之后结束,您还需要切换到%{REQUEST_URI}
而不是%{THE_REQUEST}
,其中包含URI后的数据(对于例如,HTTP/1.1
)。
试试:
RewriteCond %{REQUEST_URI} ^/admin[/]*$ [NC]
如果/admin
不在%{THE_REQUEST}
,因为它仍然有效,您不需要更改另一个检查。
完整的代码如下:
# force https:// for all except /admin
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/admin[/]*$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for /admin URLs
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/admin[/]*$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# all the others
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]