WordPress一般应用于Windows系统,但 .htaccess 是用Unix或者Linux系统搭建的相关文件,那我们该如何在Wordpress中实现 .htaccess 文件系统呢,今天就为大家带来有关 .htaccess 的十个相关技巧介绍:
1. 重定向WordPress的RSS Feed链接地址到Feedburner地址:
除了修改WP的模板文件来定制其输出的RSS Feed链接地址外,还可以使用.htaccess文件来进行设置(替换yourrssfeedlink为自己的Feedburner地址)。
/*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.feedburner.com/catswhocode [R=302,NC,L]
</IfModule>
*/
大家使用时别忘了把代码中的Feedburner地址替换为自己的
2. 使用浏览器缓存:
可以修改.htaccess文件让访问者使用浏览器缓存来优化其访问速度。
/*FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch “\.(jpg|gif|png|css|js)$”>
ExpiresActive on
ExpiresDefault “access plus 1 year”
</filesmatch>
</ifmodule>*/
3. 去除WordPress分类链接中的”/category/”:
默认情况下,WordPress的分类链接显示的样式为:
http://e-spacy.com/blog/category/tech
其实其中的category部分没有任何意义,如果想去掉它可以修改.htaccess文件(替换yourblog为自己的网址)。
RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]
4. 阻止没有referrer来源链接的垃圾评论:
设置.htaccess文件可以阻止大多数无Refferrer来源的垃圾评论机器人Bot Spammer。其会查询访问你网站的来源链接,然后阻止其通过wp-comments-post.php来进行垃圾评论。
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
5. 重定向日期格式的WP Permalink链接地址为Postname格式:
如 果你目前的Permalink地址为/%year%/%monthnum%/%day%/%postname%/ 的格式,那么我强烈推荐你直接使用/%postname%/ ,这样对搜索引擎要舒服得多。首先你需要在WordPress的后台设置输出的Permalinks格式为/%postname%/ 。然后修改.htaccess文件来重定向旧的链接,不然别人以前收藏你的网址都会转成404哦!(替换yourdomain为自己的网址)
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.yourdomain.com/$4
6. 压缩静态数据:
可以修改.htaccess文件来压缩需要访问的数据(传输后在访问端解压),从而可以减少访问流量和载入时间。