芒果介绍的这些 .htaccess 技巧,能使 WordPress 在安全性、功能性、可用性等方面得到有效加强。
1. 将 WordPress RSS 源地址重定向到 FeedBurner
要自定义 RSS 源地址,必须手工更改模板文件。使用 .htaccess 代码可免除模板的修改,快速将 RSS 源地址重定向到 FeedBurner 上:
# temp redirect wordpress content feeds to 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>
2. 移除 WordPress 链接中的 “/category/” 字段
默认情况下,WordPress 分类的固定链接格式如下:
http://www.mangguo.org/category/blog
分类的链接中 “/category/” 字段没有多少意义,完全可以通过 .htaccess 去除:
RewriteRule ^category/(.+)$ http://www.mangguo.org/$1 [R=301,L]
3. 使用浏览器缓存
通过使用浏览器缓存能有效优化页面载入时间:FileETag MTime Size
<ifmodule mod_expires.c> <filesmatch "\.(jpg|gif|png|css|js)$"> ExpiresActive on ExpiresDefault "access plus 1 year" </filesmatch> </ifmodule>
4. 压缩静态数据
压缩数据能为服务器贺客户端节约带宽:
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. 将日期形式的固定链接 (Permalink) 重定向至 /%postname%/ 形式
芒果介绍过 WordPress 固定链接的设置方法,要将日期形式的固定链接 (Permalink) 重定向至 /%postname%/ 形式,首先要在控制面板将固定链接设置为 /%postname%/ 形式,然后编辑 .htaccess 文件,加入代码:
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.mangguo.org/$4
6. 拒绝没有 Referrer 来源的垃圾评论
通过 .htaccess 的方法可以防止垃圾评论。这段代码将检查来路链接,从而拒绝垃圾评论的发布:
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]
7. 重定向网站到临时维护页面
当程序升级、主题更改、代码出错,安全漏洞等情况发生时,需要暂时重定向到维护页面,来通知访客暂时无法访问(使用以下代码请将 maintenance.html 替换为自定义的维护页面地址,将 xxx.xxx.xxx.xxx 替换为允许访问的 IP 地址):
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx
RewriteRule $ /maintenance.html [R=302,L]
8. 防止 WordPress 内容被盗链
盗链会大量消耗被盗链网站的带宽,却没有带来任何流量,严重损害被盗链网站的利益。用 .htaccess 能防止图片等文件被恶意盗链:
RewriteEngine On
#Replace ?mysite\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /image/blank.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /image/blank.jpg [L]
9. 只允许你的 IP 访问管理登录页面
如果你使用固定 IP,设置只允许你的 IP 访问管理登录页面,能保证 WordPress 的安全(xxx.xxx.xxx.xxx 为你的 IP 地址):
AuthGroupFile /dev/null AuthName "Example Access Control" AuthType Basic <LIMIT GET> order deny,allow deny from all allow from xxx.xxx.xxx.xxx </LIMIT>
10. 拒绝某些 IP 访问
通过 .htaccess 能屏蔽垃圾评论发送者对应 IP 的访问权限:
<Limit GET POST> order allow,deny deny from xxx.xxx.xxx.xxx allow from all </Limit>
英文原稿:10 awesome .htaccess hacks for WordPress | CatsWhoCode
翻译整理:WordPress 的 10 个 .htaccess 技巧 | 芒果
转载自 <a href="http://www.mangguo.org/wordpress-10-htaccess-hacks/" title="WordPress 的 10 个 .htaccess 技巧" rel="bookmark">WordPress 的 10 个 .htaccess 技巧 | 芒果小站</a>
已经有 1 条群众意见
- WordPress 的 10 个 .htaccess 技巧 @ 菜刀会
#1/2010-09-09 10:00[...] awesome .htaccess hacks for WordPress | CatsWhoCode 翻译整理:WordPress 的 10 个 .htaccess 技巧 | 芒果 « WordPress [...] 回应
我简单说几句