10段有用的.htaccess代码

来源:互联网 发布:c指针例题编程 编辑:程序博客网 时间:2024/05/20 04:08

[代码] 移除url中的www

[html] view plaincopy
  1. RewriteEngine On  
  2. RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]  
  3. RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]  
  4. Source: http://css-tricks.com/snippets/htaccess/www-no-www/  

[代码] 防止热链

[html] view plaincopy
  1. RewriteEngine On  
  2. #Replace ?mysite\.com/ with your blog url  
  3. RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]  
  4. RewriteCond %{HTTP_REFERER} !^$  
  5. #Replace /images/nohotlink.jpg with your "don't hotlink" image url  
  6. RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]  

[代码] 重定向所有的WordPress feeds 到 feedburner

[html] view plaincopy
  1. <IfModule mod_alias.c>  
  2.  RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/  
  3.  RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/  
  4. </IfModule>  
  5. Source: http://www.wprecipes.com/how-to-redirect-wordpress-rss-feeds-to-feedburner-with-htaccess  

[代码] 创建常规错误页面

[html] view plaincopy
  1. ErrorDocument 400 /errors/badrequest.html  
  2. ErrorDocument 401 /errors/authreqd.html  
  3. ErrorDocument 403 /errors/forbid.html  
  4. ErrorDocument 404 /errors/notfound.html  
  5. ErrorDocument 500 /errors/serverr.html  
  6.    
  7. Source: http://css-tricks.com/snippets/htaccess/custom-error-pages/  

[代码] 强迫指定文件下载

[html] view plaincopy
  1. <Files *.xls>  
  2.   ForceType application/octet-stream  
  3.   Header set Content-Disposition attachment  
  4. </Files>  
  5. <Files *.eps>  
  6.   ForceType application/octet-stream  
  7.   Header set Content-Disposition attachment  
  8. </Files>  
  9.    
  10. Source: http://www.givegoodweb.com/post/30/forcing-a-download-with-apache-and-htaccess  

[代码] PHP 错误日志

[html] view plaincopy
  1. # display no errs to user  
  2. php_flag display_startup_errors off  
  3. php_flag display_errors off  
  4. php_flag html_errors off  
  5. # log to file  
  6. php_flag log_errors on  
  7. php_value error_log /location/to/php_error.log  
  8.    
  9. Source: http://css-tricks.com/snippets/htaccess/php-error-logging/  

[代码] 从URL中移除文件拓展名,如.html

[html] view plaincopy
  1. RewriteEngine on  
  2. RewriteCond %{REQUEST_FILENAME} !-d  
  3. RewriteCond %{REQUEST_FILENAME}\.html -f  
  4. RewriteRule ^(.*)$ $1.html  
  5. # Replace html with your file extension, eg: php, htm, asp  
  6.    
  7. Source: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess  

[代码] 防止自动目录列表

[html] view plaincopy
  1. Options -Indexes  

[代码] 压缩静态数据

[html] view plaincopy
  1. AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml  
  2. application/xhtml+xml text/javascript text/css application/x-javascript  

[代码] 强制指定特定的编码

[html] view plaincopy
  1. <FilesMatch "\.(htm|html|css|js)$">  
  2. AddDefaultCharset UTF-8  
  3. </FilesMatch>  
  4.    
  5. Source: http://www.askapache.com/htaccess/setting-charset-in-htaccess.html  
0 0
原创粉丝点击