正则表达式

时间:2017-02-04 19:51:44

标签: regex .htaccess redirect

我使用正则表达式that's working fine重写规则。 但现在我想添加不是的重新添加< 301 301重定向。

RewriteRule ^post/[0-9]+/([^/]+)$ http://example.com/$1 [L,R=301]

RedirectMatch 301 /some-other-old-slug http://example.com/category/some-new-slug-based-on-some-other-old-slug

第一个起作用,第二个起作用。怎么来的?

编辑:更改为重定向使其正常工作。但实际上我需要重写规则和偶尔的段塞更改。例如:

example.com/post/123345/old-slug to example.com/old-slug-edited。 看起来第一个重写规则是诀窍,第二个将被忽略。

1 个答案:

答案 0 :(得分:1)

这应该可以正常工作。您不需要RedirectMatch来指定确切的URI。只需使用Redirect即可。但是我认为你在测试时只是犯了一个拼写错误,或者当你简化问题时没有通过其他一些问题。没理由这不起作用。

<强>更新

对于边缘情况,首先使用mod_rewrite处理它们。我选择在RewriteCond中执行此操作以提高易读性,因此可以粘贴URI而无需转义特殊字符。为每个边缘情况添加一对:

RewriteCond %{REQUEST_URI} =/post/123456789/edge-case
RewriteRule ^ http://example.com/new-edge-case-slug [L,R=301]

RewriteRule ^post/[0-9]+/([^/]+)$ http://domain.com/$1 [L,R=301]
相关问题