This article from: http://www.oschina.net/question/28_30385
Apache. htaccess files are the heart of the server and control various Website access rules. Here we provide 10 good. htaccess snippets to help you optimize your website, including redirection, performance, availability, and so on!
1. Force suffix backslash
Adding a backslash at the end of the URL seems to be beneficial to SEO :)
<IfModule mod_rewrite.c> RewriteCond %{REQUEST_URI} /+[^\.]+$ RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]</IfModule>
2. Anti-leech
Save your valuable bandwidth!
RewriteEngine On#Replace ?mysite\.com/ with your blog urlRewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]RewriteCond %{HTTP_REFERER} !^$#Replace /images/nohotlink.jpg with your "don't hotlink" image urlRewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
3. redirect mobile devices
If your website supports access from mobile devices, you 'd better redirect the access from mobile devices to a custom page.
RewriteEngine OnRewriteCond %{REQUEST_URI} !^/m/.*$RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]#------------- The line below excludes the iPadRewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$#-------------RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOWRewriteRule ^(.*)$ /m/ [L,R=302]
4. Force the browser to download the specified file type
You can force the browser to download some types of files, instead of reading and opening these files, such as MP3 and 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>
5. Firefox Cross-Domain font embedding
Firefox cannot embed an external font. The following. htaccess fragment can bypass this restriction.
<FilesMatch "\.(ttf|otf|eot|woff){1}quot;><IfModule mod_headers.c> Header set Access-Control-Allow-Origin "http://yourdomain.com"</IfModule></FilesMatch>
6. Use. htaccess cache to speed up the website
I'm afraid this is the most useful code snippet. This code can greatly speed up your website
# 1 YEAR<FilesMatch "\.(ico|pdf|flv){1}quot;>Header set Cache-Control "max-age=29030400, public"</FilesMatch># 1 WEEK<FilesMatch "\.(jpg|jpeg|png|gif|swf){1}quot;>Header set Cache-Control "max-age=604800, public"</FilesMatch># 2 DAYS<FilesMatch "\.(xml|txt|css|js){1}quot;>Header set Cache-Control "max-age=172800, proxy-revalidate"</FilesMatch># 1 MIN<FilesMatch "\.(html|htm|php){1}quot;>Header set Cache-Control "max-age=60, private, proxy-revalidate"</FilesMatch>
7. prevent spam comments from WordPress blogs
Is it still a headache for spam comments? You can use the akismet plug-in to solve this problem, but the. htaccess file comes more directly: Blocking spam comments robots from accessing wp-comments-post.php files.
<IfModule mod_rewrite.c>RewriteEngine OnRewriteCond %{REQUEST_METHOD} POSTRewriteCond %{REQUEST_URI} .wp-comments-post\.php*RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR]RewriteCond %{HTTP_USER_AGENT} ^$RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]</IfModule>
8. Redirect different feed formats to the unified format
Many years ago, there were many different feed formats, such as RSS, atom, and RDF. But now RSS is absolutely dominant. The following code redirects different feed formats to the same feed. This code can be used directly on the Wordpress blog.
<IfModule mod_alias.c> RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://example.com/feed/ RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/</IfModule>
9. Configure the HTML5 video of the website
HTML5 brings us the video playback function without flash, but you must configure your server to provide the latest HTML5 video playback function.
RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !=/favicon.icoAddType video/ogg .ogvAddType video/ogg .oggAddType video/mp4 .mp4AddType video/webm .webmAddType application/x-shockwave-flash swf
10. Record PHP errors
Displaying PHP errors on the page is embarrassing and insecure. The following code records PHP errors in the. Log File rather than on the page.
# display no errs to userphp_flag display_startup_errors offphp_flag display_errors offphp_flag html_errors off# log to filephp_flag log_errors onphp_value error_log /location/to/php_error.log
11. run php In JavaScript code
Inserting PHP code in JS is sometimes useful, for example, reading a database. The following code allows you to run php In Js.
AddType application/x-httpd-php .jsAddHandler x-httpd-php5 .js<FilesMatch "\.(js|php){1}quot;>SetHandler application/x-httpd-php</FilesMatch>