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

标签 .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/

我想重定向 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/

相关文章:

仅当单词(url)不包含字符时才匹配单词(url)的正则表达式

javascript - 链接不会重定向到 Jquery 选项卡

php - .htaccess 将文件路径重写为文件夹路径

php - Mod_rewrite 和不同顺序的多个变量

apache - 如何启用对 Joomla 站点根目录中我的自定义目录的直接访问?

c# - URL 用 + 号替换空格

.htaccess - OpenCart htaccess 重定向到 WWW 和 SSL

将协议(protocol)添加到url字符串的Javascript正则表达式

ruby-on-rails - 如何使用 Rails 3/Rack 在没有 www 的情况下重定向?

Java:JSP 在页面的其余部分停止执行