10个关于WordPress的.htaccess应用技巧

网络 · 2010-11-26

.htaccess是使用UNIX或Linux搭建的服务器中的一个特殊的文件,这个文件只存在于Linux系统中,Win系列的主机是没有的。那 么.htaccess有什么功能呢?通俗点的讲,就是可以通过编写这个文件中的某些内容,进而实现.htaccess文件所在目录及其子目录的权限与功能 的设置,是自己的站点灵活多变,下面就介绍.htaccess文件关于WordPress的十个应用技巧,举一反三,这些应用技巧同样适用于其它站点程序。

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

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]
*/
大家使用时别忘了把代码中的Feedburner地址替换为自己的
参考:How to redirect WordPress rss feeds to feedburner

  1. 使用浏览器缓存:
    可以修改.htaccess文件让访问者使用浏览器缓存来优化其访问速度。
    /*FileETag MTime Size

ExpiresActive on ExpiresDefault “access plus 1 year”
*/

  1. 去除WordPress分类链接中的”/category/”:
    默认情况下,WordPress的分类链接显示的样式为:
    http://e-spacy.com/blog/category/tech
    其实其中的category部分没有任何意义,如果想去掉它可以修改.htaccess文件(替换yourblog为自己的网址)。
    RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]
    参考:How to remove category from your WordPress url
  2. 阻止没有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]
    参考: How to deny comment posting to no referrer requests
  3. 重定向日期格式的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
    参考: Redirect day and name permalinks to postname
  4. 压缩静态数据:
    可以修改.htaccess文件来压缩需要访问的数据(传输后在访问端解压),从而可以减少访问流量和载入时间。
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html
  5. 阻止指定IP的访问:
    如果你想要阻止指定IP的访问,来防止其垃圾评论,那么你可以创建自己的Backlist黑名单。(替换xx.xx.xx.xx为指定的IP地址)

order allow,deny deny from xx.xx.xx.xx allow from all
参考:The easiest way to ban a WordPress spammer

  1. 只允许自己的IP访问wp-admin:
    如果你不是团队合作Blog,最好设置只有自己能够访问WP的后台。前提是你的IP不是像我一样动态的哦。(替换xx.xx.xx.xx为自己的IP地址)
    AuthUserFile /dev/null
    AuthGroupFile /dev/null
    AuthName “Example Access Control”
    AuthType Basic

order deny,allow deny from all allow from xx.xx.xx.xx
参考:Protecting the WordPress wp-admin folder

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

    Replace ?mysite.com/ with your blog url

    RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^$

    Replace /images/nohotlink.jpg with your “don’t hotlink” image url

    RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
    参考:How to protect your WordPress blog from hotlinking

  2. 定制访问者跳转到维护页面:
    当你进行网站升级,模板修改调试等操作时,最好让访问者临时跳转到一个声明的维护页面(和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]/
    自定义错误页面(直接拷贝即可)
    ErrorDocument 404 /error-pages/not-found.html
    ErrorDocument 503 /error-pages/service-unavailable.html
    ————————-
    IP禁止
    Order allow,deny
    Deny from 123.45.67.8
    Deny from 123.123.7
    Allow from all
    上面能禁止IP地址在123.45.67.8以及IP地址开头为123.123.7的任何人。例如123.123.74.42 就不能得到访问。
    ————————-
    变更默认首页
    DirectoryIndex homepage.html
    ————————-
    去除页面广告(不一定适用所有免费空间)
    LayoutIgnoreURI *.php
    LayoutIgnoreURI *.cgi
    LayoutIgnoreURI *.htm
    LayoutIgnoreURI *.html
    LayoutIgnoreURI *.txt
    ————————-
    页面跳转
    Redirect page1.html page2.html
    如果某人访问 http://www.example.com/page1.html,他将被跳转到(带有HTTP状态代码302)的http://www.example.com/page2.html
    ————————-
    服务器内置SSI
    AddType text/html .shtml
    AddHandler server-parsed .shtml
    Options Indexes FollowSymLinks Includes
    ————————-
    防止图片热链
    后面的 .htaccess规则使用了mod rewrite。
    特别域名
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} ^http://(1+.)?baddomain1.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^http://(1+.)?baddomain2.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^http://(1+.)?baddomain3.com [NC]
    RewriteRule .(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]
    非特别域名
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
    RewriteRule .(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]
    除非 example.com有这个图片,浏览器才能看到hotlink.gif.
    注意:Hotlink热链保护使用 .htaccess 依赖客户端在http GET请求中发送正确的”提交”值。像尝试使用Windows Media Player发送空白的提交到.htaccess 来保护电影档案是无效的。
    禁止.htaccess文件被查看
    在.htaccess文件中加入如下代码就可以禁止别人访问你的.htaccess文件:

order allow,deny deny from all
这个网上的大部分版本都有错误,大部分版本丢掉了

,结果导致所有文件都被禁止访问。如果用了错误的规则,所有内容都将无法访问。 同样道理,如果要禁止其他文件的访问,用其他文件名替换就可以了。


  1. /
Powered by Typecho Theme Jasmine 冀ICP备15024791号-3