.htaccess : redirect permanent and stop new rules

时间:2015-10-06 08:43:43

标签: .htaccess mod-rewrite

I have that rule that works well

RedirectMatch 301 ^/(.*?)-/(.*)$ /$1/$2

It redirects for instance

http://example.com/category-/list/town/id/id2/country ->

http://example.com/category/list/town/id/id2/country

Problem is, later .htaccess adds some extra query parameters.

My goal is: when this rule is matches, then do not apply other .htaccess rules

I tried with:

RewriteRule ^/(.*?)-/(.*)$ /$1/$2 [R=301,L]

and rule is not applied at all !

1 个答案:

答案 0 :(得分:1)

在注释中提到@hjpotter92时,模式是而不是 .htaccess文件中的前导斜杠。见Apache mod_rewrite Technical Details

  

在每个目录上下文中(即在.htaccess文件和目录块中),在URL已经转换为文件名后应用这些规则。因此,mod_rewrite最初比较RewriteRule指令的URL路径是已翻译文件名的完整文件系统路径,其中当前目录路径(包括尾部斜杠)从前面删除。

     

举例说明:如果规则在/var/www/foo/.htaccess中并且正在处理/foo/bar/baz的请求,则^bar/baz$之类的表达式将匹配。

RewriteRule在“&-34; Per-directory Rewrites"”一节中有类似的说明。

所以你的规则应该是

RewriteRule ^(.*?)-/(.*)$ /$1/$2 [R,L]

如果您拥有此RewriteRule,则不再需要RedirectMatch