.htaccess文件使用技巧汇总
转自:http://www.sayblog.me/tips-to-use-htaccess-file.html
.htaccess是Web服务器Apache 中特有的一个配置文件,操控着服务器上的许多行为,我们可以利用它来做许多事情,例如:设置访问权限,网址重定向,等等。本文向大家展示.htaccess文件的使用技巧,欢迎各位收藏备用。
温馨提示:在编辑.htaccess文件时,切记一定要先做好备份,因为这个文件相当重要,编辑错了可能会出现不可想像的后果!
1、去除博客网址中的WWW.
据说这样做有利于SEO。将下面的代码加入到.htaccess文件中,结果会把所有带www.的网址重定向到不带www.的网址。需要将your-site.com改成您的域名哦!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]来源: css-tricks
2、强制使用WWW.格式的网址
将www.your-site.com改成您的域名哦!
RewriteEngine OnRewriteCond %{HTTP_HOST} ^your-site.com [NC]RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301]来源: css-tricks
3、设置图片防盗链
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]详细可参考这一篇:设置Wordpress图片防盗链的方法
4、重定向WordPress博客中的所有feeds到feedburner
将http://feedburner.com/yourfeed/ 换成您的feedburnerID号。
<IfModule mod_alias.c>
RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/
RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/
</IfModule>
5、自定义错误页面
需要您自己创建一些html文件,访问者访问不存在的页面时就会显示这些html页面。
ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html来源: css-tricks
6、强制下载特定的文件
如果您的网站提供mp3s, eps 或 xls此类文件的下载,要是不想让浏览器来决定是否下载,利用如下代码可强制下载。
<Files *.xls>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
<Files *.eps>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>来源: givegoodweb
7、PHP错误记录
在您的服务器上创建一个 php_error.log文件,再将以下代码放入.htaccess文件中,记得修改第七行中的文件路径。这是一条很有用的函数。
# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log来源: css-tricks
8、从网址中去除文件的扩展名
文件扩展名对网页开发者很有用,但对访问的人没用,您也不希望他们看到的,使用下面的方法将它们去掉。下面是去掉html扩展名的方法,可以改成php, htm, asp 等。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp来源:eisabainyo
9、禁止自动列出系统文件目录
如果您网站某个文件目录不存在index文件,服务器会自动列出当前目录中的所有文件,通常您并不希望让别人看到,使用下面这句代码,可以禁止目录被列出。
Options -Indexes
10、去除WordPress网址中的/category/目录名称
默认情况下, WordPress分类目录的永久链接会显示如下:
http://www.catswhocode.com/blog/category/wordpress其中的目录名/category/毫无用处,可以去掉,只需在.htaccess文件中加上下面这一行(其中http://www.yourblog.com/是您的域名):
RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]将修改后的文件保存,分类目录的链接形式就会变成如下:
http://www.yourblog.com/blog/wordpress
11、使用浏览器缓存
一个有效加快博客速度的方法就是,强制浏览器缓存。在.htaccess文件中添加以下语句:
FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch ".(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
12、将带发表日期和日志名称的永久链接重定向到 /%postname%/格式
这个/%postname%/就是日志名称,也就是使用日志名称作为永久链接。首先登录WordPress后台,到设置→ 永久链接,在自定义永久链接这一项中填上
/%postname%/,之后您的博客永久链接会变成如下这样:
http://www.yourblog.com/name-of-the-post接着下来重要的一步,利用301重定向将所有旧的链接全部指向上面所建新链接。打开WordPress根目录下的.htaccess文件,加入以下一行语句:
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4搞定了!
13、拒绝非人为的发布评论
原理是禁止(机器人)非人为访问wp-comments-post.php文件, 可有效防止大量的垃圾评论。将第四行的yourblog.com改成您的网址即可。
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]
14、将访问者重定向到一个维护模式页面
在升级博客或者修改主题的时候,让访客看到这些变化(特别是代码错误),总不是件好事,甚至可能引发安全问题,这时,就有必要将所有访问者引向一个指定的维护模式页面。下面的语法中使用302重定向,将读者引导到一个叫做”maintenance.html“的页面,第三行中是您自己博客的IP地址。
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123.123.123.123
RewriteRule $ /maintenance.html [R=302,L]来源: woueb
15、仅允许您自己的IP访问wp-admin目录
除非是多作者博客,如果您使用的是静态IP,可以设定只有您自己可以访问wp-admin目录,使用“allow from xx.xx.xxx.xx“ 这样的格式,多个静态IP各占一行。
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Example Access Control"
AuthType Basic
<LIMIT GET>
order allow, deny
deny from all
allow from xx.xx.xx.xx
</LIMIT>来源: reubenyau
16、将特定的IP列入黑名单
使用“deny from xxx.xx.xxx.xxx”这样的格式,不同的IP各占五行,这种方法可用于防止垃圾评论或某些您想禁止访问的访客。
<Limit GET POST>
order allow,deny
deny from 222.69.116.166
allow from all
</Limit>
17、压缩静态文件,加速页面下载
这样会在一定程度上减少服务器带宽消耗,加快读者访问速度。
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给文件自动添加utf-8编码
对于某些特定的文件,如html, htm,css或js等,有时可能会忘记在页面中加上一条<meta http-equiv=”Content-Type”> 编码标签,为防止出现编码问题,可让页面自动生成这样的标签。
<FilesMatch ".(htm|html|css|js)$">
ForceType 'text/html; charset=UTF-8'
</FilesMatch>