mod_rewrite的 - 排除网址 [英] mod_rewrite - exclude urls
问题描述
我需要一个mod_rewrite的重定向所有 HTTP
要求 HTTPS
,但我想你排除某些网址
I need a mod_rewrite to redirect all http
requests to https
, but I want do exclude some URLs
# force https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\. [NC]
RewriteCond %{REQUEST_URI} !gateway_callback [NC]
RewriteRule ^. https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
匹配所有网址 gateway_callback
必须排除
此网址不应该被重定向,但它确实!?
This URL should not be redirected, but it does!?
http://secure.localhost/da/gateway_callback/29/
试过刷新DNS缓存的浏览器,但该网址仍重定向到 HTTPS
推荐答案
与你的配置的主要问题是REQUEST_URI变量包含后的一切,包括斜杠。第三个发言的RewriteCond需要更新到类似如下:
The main problem with your config is that the REQUEST_URI variable contains everything after and including the forward slash. The third RewriteCond statement needs to be updated to something like the following:
RewriteCond %{REQUEST_URI} !^/da/gateway_callback/.*$ [NC]
这应该与您提供的例子。如果URI并不总是与启动 / DA /
,那么你可能需要把一个通配符:
This should match the example you have provided. If the URI does not always start with /da/
then you might need to put in a wildcard:
RewriteCond %{REQUEST_URI} !^/[^/]+/gateway_callback/.*$ [NC]
其中 [^ /] +
匹配一个或多个字符,这不是一个斜杠。
where the [^/]+
matches one or more characters which is not a forward slash.
我总是会用正则表达式锚尽可能因为它消除歧义建议。原来的RewriteCond试图匹配REQUEST_URI不使用它们,可在一个偶然一瞥迷惑管理员。
I would recommend always using the regex anchors wherever possible as it removes ambiguity. The original RewriteCond attempting to match REQUEST_URI does not use them, which can confuse admins at a casual glance.
另外请注意,在的RewriteCond 和重写规则
指令所有相关的例子: //httpd.apache.org/docs/2.2/mod/mod_rewrite.html相对=nofollow>官方文档使用开始锚。
Also note that all related examples for the RewriteCond
and RewriteRule
directives in the official documentation use the start anchor.
这篇关于mod_rewrite的 - 排除网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!