注意:以下文章翻译来自英文网站:10 awesome .htaccess hacks for WordPress
.htaccess 文件是用来控制 Apache 服务器的,它很有用,并允许你做很多事,下面就介绍关于.htaccess 的 10 个修改方法,让你的 wordpress 更加的安全、多功能、和可用性!
警告:在修改之前请你备份.htaccess,一边修改后出现错误你可以恢复!
1. 重定向你的 WordPress RSS feeds 到 feedburner 或 feedsky
1 2
3
4
5
6
|
# temp redirect wordpress content feeds to feedburner
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http:// 在 feedburner 你的地址 [R=302,NC,L]
|
2. 删除博客中的 /category/你是不是觉得博客分类浏览 url 老有个 /category/ 觉得很不滑爽?要移除 /category/,除了可以用插件 Top Level Categories 来实现,也可以通过修改.htaccess 来实现!
打开.htaccess,在里边添加:一般是紧跟在最后一行 RewriteRule ^ 字样的下面添加
1
|
RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]
|
注意:记得要把 http://www.yourblog.com/ 替换为你的网址
3. 使用浏览器的缓存
强制浏览器使用缓存,加速网页载入时间,当然是在网页没有改变的前提下
1 2
3
4
5
|
FileETag MTime Size
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
|
4. 压缩静态数据
此代码肯定会节省您(和您的访客)带宽
1 2
3
4
|
AddOutputFilterByType DEFLATE text/html text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
|
5. 重定向”day”和”name”到 /%postname%/
如果你使用的固定链接为”/%year%/%monthnum%/%day%/%postname%/”, 你又想把固定链接设置为”/%postname%/”, 又怕以前的链接失效,那这个可以帮助你解决!
首先把你的固定链接设置成 /%postname%/,然后在你的.htaccess 添加:
1
|
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4
|
记得把 www.domain.com 替换成你的域名!
6. 如果拒绝带网页链接评论
这个功能你可以使用 akismet 插件来完成,当然也可以通过修改.htaccess 来完成!
1 2
3
4
5
6
|
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]
|
记得把 yourblog.com 替换成你的域名!
7. 如何重定向访问者到一个固定页面
有些时候你的博客要维护,你想把你的博客的访问者重定向到一个维护的说明页面,这个就能然你实现!
1 2
3
4
|
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
RewriteRule $ /maintenance.html [R=302,L]
|
maintenance.html 这个就是你要重定向到的页面
8. 拒绝引用你的博客的图片
有些时候由于其他网站引用你博客的图片使得你的流量暴增,这个功能将是别人无法引用你的图片!
1 2
3
4
5
6
|
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]
|
记得把 mysite.com 修改成你的域名,nohotlink.jpg 为别人引用你图片时在他网站出现的图片!
9. 只允许你自己的 Ip 地址才可以登录到 wp-admin 管理后台
其实这个也很有用,只是我们这些使用 ADSL 的无法拥有固定 Ip,所以这个功能就有点无奈!
1 2
3
4
5
6 7
8
|
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Example Access Control"
AuthType Basic
order deny,allow
deny from all
allow from xx.xx.xx.xx
|
xx.xx.xx.xx 为你的 IP 地址!10. 禁止指定的 IP 地址访问
有些时候你的博客可能受到同一个 Ip 地址的骚扰,比如说垃圾短信,你可以用禁止此 IP 访问的办法来抵制!
1 2
3
|
order allow,deny
deny from 200.49.176.139
allow from all
|
正文完