当前位置: 首页 > 图文教程 > 网站运营 > 建站经验 > 10个WordPress的.htaccess技巧(强烈推荐)

建站经验
一个小站长失败的建站之路
Bo-Blog博客搬家与数据转移的方法
做好SEO推广 网站流量不是难事
放弃网站到网商 评站长与网商的相同与不同
《一日一囧》式博客营销:如何培养用户粘度
谈谈我用过的博客(Blog)程序
再谈Music2.0互联网音乐调查报告
国内几个价值流量的来源
新站长选择CMS请勿犹豫徘徊 自己熟悉为先
分类信息网会成为互联网主要应用吗
网站案例分析:[jiong.ws]网络囧文化的背后
放弃工作 我辛酸的建站创业史
20个增加博客粘合度的方法
交互体验在web2.0网站设计中四个层次
一个只有小学文化的站长 建立影视站的经验
西部硅谷网CEO潘陶:我的创业历程
不经历风雨怎么能见彩虹:我与马克斯的不解之缘
新站确保被收录的因素分析
WordPress主题分享 让你的博客也成为CMS
如何通过IIS日志分析网站的情况

建站经验 中的 10个WordPress的.htaccess技巧(强烈推荐)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 109 ::
收藏到网摘: n/a

对于Apache服务器,使用.htaccess文件可以进行很多相关网络服务访问的配置。而以下的10个技巧则专门针对WordPress所进行的设置,推荐大家参考使用:

参考原文:10 awesome .htaccess hacks for WordPress

1. 重定向WordPress的RSS Feed链接地址到Feedburner地址:

除了修改WP的模板文件来定制其输出的RSS Feed链接地址外,还可以使用.htaccess文件来进行设置(替换yourrssfeedlink为自己的Feedburner地址)。

1.# temp redirect wordpress content feeds to feedburner
2.<IfModule mod_rewrite.c>
3. RewriteEngine on
4. RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
5. RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
6. RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.feedburner.com/catswhocode [R=302,NC,L]
7.</IfModule>

 

参考:How to redirect WordPress rss feeds to feedburner

2. 去除WordPress分类链接中的“/category/”:

默认情况下,WordPress的分类链接显示的样式为:

http://e-spacy.com/blog/category/tech

其实其中的category部分没有任何意义,如果想去掉它可以修改.htaccess文件(替换yourblog为自己的网址)。

1.RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]

参考:How to remove category from your WordPress url

3. 使用浏览器缓存:

可以修改.htaccess文件让访问者使用浏览器缓存来优化其访问速度。

1.FileETag MTime Size
2.<ifmodule mod_expires.c>
3.  <filesmatch "\.(jpg|gif|png|css|js)$">
4.       ExpiresActive on
5.       ExpiresDefault "access plus 1 year"
6.   </filesmatch>
7.</ifmodule>

 

 

 

参考: Comment accelerer le temps de chargement de votre blog

4. 压缩静态数据:

可以修改.htaccess文件来压缩需要访问的数据(传输后在访问端解压),从而可以减少访问流量和载入时间。

1.AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
2.BrowserMatch ^Mozilla/4 gzip-only-text/html
3.BrowserMatch ^Mozilla/4.0[678] no-gzip
4.BrowserMatch bMSIE !no-gzip !gzip-only-text/html

 

5. 重定向日期格式的WP Permalink链接地址为Postname格式:

如果你目前的Permalink地址为/%year%/%monthnum%/%day%/%postname%/ 的格式,那么我强烈推荐你直接使用/%postname%/ ,这样对搜索引擎要舒服得多。首先你需要在WordPress的后台设置输出的Permalinks格式为/%postname%/ 。然后修改.htaccess文件来重定向旧的链接,不然别人以前收藏你的网址都会转成404哦!(替换yourdomain为自己的网址)

1.RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.yourdomain.com/$4

参考: Redirect day and name permalinks to postname

6. 阻止没有referrer来源链接的垃圾评论:

设置.htaccess文件可以阻止大多数无Refferrer来源的垃圾评论机器人Bot Spammer。其会查询访问你网站的来源链接,然后阻止其通过wp-comments-post.php来进行垃圾评论。

1.RewriteEngine On
2.RewriteCond %{REQUEST_METHOD} POST
3.RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
4.RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR]
5.RewriteCond %{HTTP_USER_AGENT} ^$
6.RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

 

参考: How to deny comment posting to no referrer requests

7. 定制访问者跳转到维护页面:

当你进行网站升级,模板修改调试等操作时,最好让访问者临时跳转到一个声明的维护页面(和404错误页面不同),来通知网站暂时无法访问,而不是留下一片空白或者什么http bad错误。(替换maintenance.html为自己定制的维护页面网址,替换123.123.123.123为自己目前的IP地址,不然你自己访问也跳转哦)

RewriteEngine on

RewriteCond %{REQUEST_URI} !/maintenance.html$

RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123

RewriteRule $ /maintenance.html [R=302,L]

参考:Comment faire une page d’accueil pour les internautes

8. 设置你的WordPress防盗链:

盗链是指其它网站直接使用你自己网站内的资源,从而浪费网站的流量和带宽,比如图片,上传的音乐,电影等文件。(替换mysite为自己的网址和/images/notlink.jpg为自己定制的防盗链声明图片)

1.RewriteEngine On
2.#Replace ?mysite\.com/ with your blog url
3.RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
4.RewriteCond %{HTTP_REFERER} !^$
5.#Replace /images/nohotlink.jpg with your "don't hotlink" image url
6.RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

参考:How to protect your WordPress blog from hotlinking

9. 只允许自己的IP访问wp-admin:

如果你不是团队合作Blog,最好设置只有自己能够访问WP的后台。前提是你的IP不是像我一样动态的哦。(替换xx.xx.xx.xx为自己的IP地址)

AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Example Access Control"
AuthType Basic
<LIMIT GET>
order deny,allow
deny from all
allow from xx.xx.xx.xx
</LIMIT>

参考:Protecting the WordPress wp-admin folder

10. 阻止指定IP的访问:

如果你想要阻止指定IP的访问,来防止其垃圾评论,那么你可以创建自己的Backlist黑名单。(替换xx.xx.xx.xx为指定的IP地址)

  1. <Limit GET POST>
  2. order allow,deny
  3. deny from xx.xx.xx.xx
  4. allow from all
  5. </Limit>

参考:The easiest way to ban a WordPress spammer