10个经典.htaccess应用
10大经典htaccess应用 1、去掉网站链接(url)中的www 代码:
RewriteEngine On RewriteCond %{HTTP_HOST} !^your-site.com$ [NC] RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]
2、防盗链设置 代码:
RewriteEngine On RewriteCond %{HTTP_HOST} !^your-site.com$ [NC] RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]
更详尽的防盗链方法请点击“利用.htaccess防盗链设置”
3、重定向RSS/Feed 到Feedburner
代码:
RedirectMatch 301 /feed/(atomrdfrssrss2)/?$ http://feedburner.com/yourfeed/ RedirectMatch 301 /comments/feed/(atomrdfrssrss2)/?$ http://feedburner.com/yourfeed/
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
详情请看“漂亮的404页面”
5、强制现在特定的文件 有些浏览器对xls、eps、html、txt、xml等类型的文件默认是打开,而我们又想让用户下载,那么此时就需要进行强制下载的设置 代码:
ForceType application/octet-stream Header set Content-Disposition attachment ForceType application/octet-stream Header set Content-Disposition attachment
6、php错误日志 这是一个有趣的添加php错误日志的方式,你只要在服务器的某个位置创建php_error.log文件,并且将此文件的路径添加到htaccess文件就OK了 代码:
# 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、隐藏服务器中文件列表 如果你不想让别人看到你的目录文件,不想让人知道你的文件列表,那么就可以用下面一句代码实现 代码:
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编码集到文件 自动添加utf-8编码到文件,防止因编码问题出现乱码现象 代码:
AddDefaultCharset UTF-8
原文来源:http://www.catswhocode.com/blog/10-useful-htaccess-snippets-to-have-in-your-toolbox