【问题标题】:RewriteRule apply on particular directoryRewriteRule 适用于特定目录
【发布时间】:2020-05-16 11:24:34
【问题描述】:

这是.htaccess中使用的代码,

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

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

【问题讨论】:

  • 你只需要在规则之前添加一个 RewriteCond 以仅在满足条件时应用它
  • 到目前为止,您做了哪些尝试来完成这项工作?为什么用 PHP 和“getmethod”标记而不用 htaccess 标记?

标签: php getmethod


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多