将请求重定向到与模式不匹配的目录

时间:2013-03-21 19:33:06

标签: .htaccess url redirect

我有以下网址

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/

我想重定向网址1,2和3,但不是4和5.换句话说,我想重定向/articles/目录中不以wp-开头的所有网页

以下代码段将重定向/articles/

之后的所有内容
Options +FollowSymlinks
RewriteEngine on
# RedirectMatch 301 /articles/(.*) http://domain.com/$1

如何过滤此重定向匹配以排除模式?

修改以回应一些评论

您正在测试的输入网址(来自浏览器)

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

1 个答案:

答案 0 :(得分:2)

使用RedirectMatch来验证URI不是以RewriteCond开头并执行重写,而不是/articles/wp-

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]