【问题标题】:Restrict some url pattern in a apache redirect在 apache 重定向中限制某些 url 模式
【发布时间】:2014-01-17 10:30:31
【问题描述】:

我正在编写一个重定向来匹配模式 /message/* 这里的任何 url。

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

现在我想通过在 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.

我尝试了以下方法。但我不确定它们是否正确。

RewriteCond %{THE_REQUEST} !^/message/index

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

有人能告诉我怎么做吗?

【问题讨论】:

标签: apache .htaccess mod-rewrite redirect url-rewriting


【解决方案1】:

%{THE_REQUEST} 包含一个类似于常规页面请求的字符串:

GET /message/index.html HTTP/1.1

%{REQUEST_URI} 看起来像这样:

/message/index.html

所以你的第二个选项几乎是正确的。对于 RewriteRules,您不需要在 Pattern 开头使用 /

另外,这两条规则会阻止所有以/message/index开头的请求被重定向):

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

如果您只想阻止/message/index.html 而不是/message/index.php/message/index-of-something-else,请执行以下操作:

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

【讨论】:

  • 感谢您的详细解答。
猜你喜欢
  • 2011-04-29
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
相关资源
最近更新 更多