前端集合 - 关注前端技术和互联网免费资源

关注前端技术和互联网免费资源

分类儿

页面儿

搜索儿

前端集合 RSS订阅
Home » 技巧应用 » .htaccess技巧十则

.htaccess技巧十则

发布者:前端集合 // 发布时间:2011-05-31 21:28:51 // 分类:技巧应用 // 访问: 3,847 次 // 热度:

.htaccess, 我想经常折腾WordPress或接触过Apache服务器的人再熟悉不过了。它是Apache服务器上用来控制文件的一个特殊工具。通过它,你可以做很多事情:

1. 去除网址中的WWW

说明:这样做更有利于SEO。

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

2. 防盗链

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]

3. 把所有WordPress feeds重定向到feedburner

<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>

4. 创建自定义错误页面

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

5. 强制下载特定类型文件

说明:有时,我们在自己的博客中放置了一些mp3、xls文件。一般情况下,当你点击含有这些文件的链接时,浏览器会让你选择是 打开在线浏览文件还是下载保存文件等。通过这条技巧,可以强制直接下载这些文件。

<Files *.mp3>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>
<Files *.xls>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>

6. 记录php错误

# 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

7. 移除URL的文件类型

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp

8. 防止目录列表直接显示

说明:如果Apache服务器上某一个目录下没有index文件,那么Apache会自动显示该目录下的所有文件。如果你不想让访客看到该目录下 到底有些什么文件,以及为了保护文件安全,那么就可以使用下面这条技巧。

Options -Indexes

9. 通过压缩静态数据来减小页面大小

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

10. 自动给文件添加utf-8格式编码

<FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>
VIA

Tags: .htaccess, 防盗链, 重定向

周星星&mdash;&mdash;你不是真的快乐>>  << 13大css3 快速开发工具
Top