Restrict some url pattern in a apache redirect

旧城冷巷雨未停 提交于 2019-12-13 03:58:00

问题


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]


来源:https://stackoverflow.com/questions/21183595/restrict-some-url-pattern-in-a-apache-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!