我有以下网址
1) domain.com/articles/one-two-three
2) domain.com/articles/four-five-six
3) domain.com/articles/{name of a post}
4) domain.com/articles/wp-content/
5) domain.com/articles/wp-admin/
我想重定向 URL 1、2 和 3,但不是 4 和 5。换句话说,我想重定向 /articles/
目录中不以 开头的所有页面wp-
下面的代码片段将重定向 /articles/
之后的所有内容
Options +FollowSymlinks
RewriteEngine on
# RedirectMatch 301 /articles/(.*) http://domain.com/$1
如何过滤此重定向匹配以排除模式?
编辑回应一些评论
您正在测试的输入 URL(来自浏览器)是什么,
http://www.chipkin.com/articles/steam-boilers-vs-hydronic-boilers/
预期输出是什么?
http://www.chipkin.com/steam-boilers-vs-hydronic-boilers/
由于这似乎是 WordPress,因此请发布您在 .htaccess 中可能已有的任何其他规则,因为可能会发生冲突。
RewriteEngine On
# If the request doesn't start /articles/wp-
RewriteCond %{REQUEST_URI} !^/articles/wp-
# Rewrite it:
RewriteRule ^articles/(.*) http://www.chipkin.com/$1 [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
最佳答案
使用 RewriteCond
来验证 URI 是否不以 /articles/wp-
开头,而不是使用 RedirectMatch
并执行重写:
RewriteEngine On
# If the request doesn't start /articles/wp-
RewriteCond %{REQUEST_URI} !^/articles/wp-
# Rewrite it:
RewriteRule ^articles/(.*) http://domain.com/$1 [L,R=301]
关于.htaccess - 将请求重定向到与模式不匹配的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556454/