htaccess snippets


/ Published in: Apache
Save to your folder(s)

.htaccess snippets to optimize your website in many ways: Redirections, performances, ease of use


Copy this code and paste it in your HTML
  1. #force trailing slash
  2. <IfModule mod_rewrite.c>
  3. RewriteCond %{REQUEST_URI} /+[^\.]+$
  4. RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
  5. </IfModule>
  6.  
  7. #prevent hotlinking
  8. RewriteEngine On
  9. #Replace ?mysite\.com/ with your blog url
  10. RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
  11. RewriteCond %{HTTP_REFERER} !^$
  12. #Replace /images/nohotlink.jpg with your "don't hotlink" image url
  13. RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
  14.  
  15. #redirect mobile devices
  16. RewriteEngine On
  17. RewriteCond %{REQUEST_URI} !^/m/.*$
  18. RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
  19. RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
  20. RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
  21. RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
  22. RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
  23. RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
  24. RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
  25. RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
  26. RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
  27. RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
  28. #------------- The line below excludes the iPad
  29. RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$
  30. #-------------
  31. RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
  32. RewriteRule ^(.*)$ /m/ [L,R=302]
  33.  
  34.  
  35. #Force download of a specific filetype
  36. <Files *.xls>
  37. ForceType application/octet-stream
  38. Header set Content-Disposition attachment
  39. </Files>
  40. <Files *.eps>
  41. ForceType application/octet-stream
  42. Header set Content-Disposition attachment
  43. </Files>
  44.  
  45. #Cross Domain Font embedding for Firefox
  46. <FilesMatch "\.(ttf|otf|eot|woff)$">
  47. <IfModule mod_headers.c>
  48. Header set Access-Control-Allow-Origin "http://yourdomain.com"
  49. </IfModule>
  50. </FilesMatch>
  51.  
  52. #Speed up your site with .htaccess caching
  53. # 1 YEAR
  54. <FilesMatch "\.(ico|pdf|flv)$">
  55. Header set Cache-Control "max-age=29030400, public"
  56. </FilesMatch>
  57. # 1 WEEK
  58. <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
  59. Header set Cache-Control "max-age=604800, public"
  60. </FilesMatch>
  61. # 2 DAYS
  62. <FilesMatch "\.(xml|txt|css|js)$">
  63. Header set Cache-Control "max-age=172800, proxy-revalidate"
  64. </FilesMatch>
  65. # 1 MIN
  66. <FilesMatch "\.(html|htm|php)$">
  67. Header set Cache-Control "max-age=60, private, proxy-revalidate"
  68. </FilesMatch>
  69.  
  70. #Stop spam on your WordPress blog
  71. <IfModule mod_rewrite.c>
  72. RewriteEngine On
  73. RewriteCond %{REQUEST_METHOD} POST
  74. RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
  75. RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR]
  76. RewriteCond %{HTTP_USER_AGENT} ^$
  77. RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
  78. </IfModule>
  79.  
  80. #Redirect different feeds to a single format
  81. <IfModule mod_alias.c>
  82. RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://example.com/feed/
  83. RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/
  84. </IfModule>
  85.  
  86. #Configure your website for HTML5 videos
  87. RewriteCond %{REQUEST_FILENAME} !-f
  88. RewriteCond %{REQUEST_FILENAME} !-d
  89. RewriteCond %{REQUEST_URI} !=/favicon.ico
  90. AddType video/ogg .ogv
  91. AddType video/ogg .ogg
  92. AddType video/mp4 .mp4
  93. AddType video/webm .webm
  94. AddType application/x-shockwave-flash swf
  95.  
  96. #Log PHP errors
  97. # display no errs to user
  98. php_flag display_startup_errors off
  99. php_flag display_errors off
  100. php_flag html_errors off
  101. # log to file
  102. php_flag log_errors on
  103. php_value error_log /location/to/php_error.log
  104.  
  105. #Run PHP inside JavaScript files
  106. AddType application/x-httpd-php .js
  107. AddHandler x-httpd-php5 .js
  108. <FilesMatch "\.(js|php)$">
  109. SetHandler application/x-httpd-php
  110. </FilesMatch>

URL: http://www.catswhocode.com/blog/10-htaccess-snippets-to-optimize-your-website

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.