如何使用htaccess自定义wordpress 404页面

November 1st, 2011

为什么要自己定义一个404页面呢,wordpress的模板不是自带404.php来处理不存在的页面了吗?我想说的是,这种404页面确实方便,但当你的网站做的时间长了,很有可能会出现大量的404页面,比如以前是做一个资讯类的网站,当改版成 wordpress博客之后,就会出现大量的404页面,而搜索引擎则会继续访问这些不存在的页面,这样增加了服务器的负担,而使用htaccess自定义一个静态的404页面,则可以解决这个问题。使用htaccess自定义wordpress 404页面那还不简单,直接在htaccess文件增加代码ErrorDocument 404 /404.html,然后新建一个404.html文件上传到根目录就OK了。但是这种方法并不可行,因为默认的重写规则是把所有的请求指向 index.php脚本,当然也包括不存在的页面,如果直接添加常规的404页面设置代码,不会起任何效果的。说了这么多,相信你已经明白为什么要为 wordpress自定义一个404页面了。下面我们来介绍如何自定义404页面。

1、第一步当然是制造一个404.html文件,并上传到网站根目录

2、删除原来的重写规则文件htaccess,添加下面如下规则。

# BEGIN WordPress
ErrorDocument 404 /404.html
RewriteEngine On RewriteBase /
RewriteRule ^archives/.*$ /index.php [L]
RewriteRule ^feed.*$ /index.php [L]
RewriteRule ^page/.*$ /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# END WordPress

archives是文章的url格式,如/archives/88,可以根据自己实际的文章url修改。如果你的博客是安装在wordpress目录,则必须在后面增加
RewriteRule ^wordpress/.*$ /index.php [L]。

This entry was posted on Tuesday, November 1st, 2011 at 10:18 am and is filed under 建站经验. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

Comments are closed.