避免在WordPress中覆盖htaccess

时间:2013-04-09 14:48:05

标签: wordpress .htaccess redirect

我在htaccess中有以下代码。

# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]

# Add this line
RewriteCond %{REQUEST_URI}    !/document         [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]

# Add these lines
RewriteCond %{REQUEST_URI}  !search\.php         [NC]
RewriteCond %{REQUEST_URI}  /document/([^/]+)/?  [NC]
RewriteRule .*  /wordpress/search.php?id=%1      [L,NC]

</IfModule>
# END Wordpress

#Add this /这些行下的行是我的自定义代码,用于将http://example.com/document/123等任何网址重定向到http://example.com/search.php?id=123

现在我想修改此代码以避免在更新/安装WordPress /插件后我的自定义规则被WordPress覆盖。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

将第二个附加块移到WordPress部分之前。重写规则中的[L]标志应该可以防止评估任何剩余的条件和规则。您不需要第一个添加的行。

# Your additions
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI}  !search\.php         [NC]
RewriteCond %{REQUEST_URI}  /document/([^/]+)/?  [NC]
RewriteRule .*  /wordpress/search.php?id=%1      [L,NC]
</IfModule>

# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]

</IfModule>
# END Wordpress