首页 > 解决方案 > RewriteRule 适用于特定目录

问题描述

这是.htaccess中使用的代码,

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /blog/index.php?a=$1 [NC,L]

我想将它应用于特定目录,我无法应用此规则,但目前它会将未找到的整个网站的 url 重定向到domain/blog/index.php

标签: phpgetmethod

解决方案


使用 RewriteCond。当您想将规则应用于特定条件时。

例如,

# Include in the next line all folders to include
RewriteCond %{REQUEST_URI}  (folder1|folder2|folder3) [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /blog/index.php?a=$1 [NC,L]

或通过添加一个相反的方式!

# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI}  !(folder1|folder2|folder3) [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /blog/index.php?a=$1 [NC,L]

另外,请记住,在某些情况下,Wordpress 会忽略 htaccess 文件

在某些情况下,将自定义内容添加到块中会阻止这种情况,但并非总是如此。这取决于正在使用的插件。

## BEGIN - My Custom Redirects
<IfModule mod_rewrite.c>
.....
</IfModule>
## END - My Custom Redirects

推荐阅读