Restrict some url pattern in a apache redirect

2019-08-16 02:25发布

问题:

I am writing a redirect to match any url of the patter /message/* here.

RewriteRule ^/message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]

Now I want to modify it by not allowing some string pattern in url.

RewriteCond to check /message/index.html in the url. 

1. Check if the request url contains /message/index.html.
2. If the condition is not met then do a redirect.

I tried the following methods. But I am not sure whether they are correct or not.

RewriteCond %{THE_REQUEST} !^/message/index

RewriteCond %{REQUEST_URI} !^/message/index [NC]

Could some one tell how to do this.

回答1:

%{THE_REQUEST} contains a string that looks like this for a regular page request:

GET /message/index.html HTTP/1.1

And %{REQUEST_URI} looks like this:

/message/index.html

So your 2nd option is almost correct. You don't need a / at the start of the Pattern for RewriteRules.

Additionally, these two rules will prevent all requests that start /message/index from being redirected):

RewriteCond %{REQUEST_URI} !^/message/index [NC]
RewriteRule ^message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]

If you only want to prevent /message/index.html and not /message/index.php or /message/index-of-something-else then do:

RewriteCond %{REQUEST_URI} !^/message/index\.html [NC]
RewriteRule ^message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]