给WordPress重定向文件.htaccess新增重定向规则

今天想给WordPress重定向文件.htaccess新增重定向规则,实现直接从数据库中读取文章生成sitemap的功能(参见《自动生成网站地图sitemap的php程序》)。但发现直接修改.htaccess文件总是有问题,解决方法分享一下。

首先,WordPress的重定向文件.htaccess代码如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

非常简单的代码,却能够实现wordpress里的各种重定向。其实.htaccess的功能很简单,就是把所有的请求多重定向到index.php文件,然后通过index.php来返回请求的内容。

所以,如果想在.htaccess文件中增加新的重定向规则的话,必须加在以上代码的前面,同时重定向语句的最后面一定加上参数[L],表明是之后不再进行重定向跳转,避免无限循环的重定向文件错误。

修改后的.htaccess如下:

RewriteEngine On
RewriteRule ^sitemap?([0-9/-]+).xml$ sitemap.php?page=$1 [L]

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

微信扫一扫 或 点击链接联系我

发表评论