The htaccess file may be small in size but is mighty in purpose. Many sites use it nowadays quite dynamically to create SEO friendly URLs on-the-fly so I have included a selection of some of the more common and useful ones. If you have any more be sure to let us know. Be careful for the yourdomain.com segments and remember to change them.
Force SSL
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
Redirect www to non-www
RewriteEngine On RewriteBase / RewriteCond % ^www.yourdomain.com [NC] RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Redirect non-www to www
RewriteEngine On RewriteBase / RewriteCond % ^yourdomain.com [NC] RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Caching Files To Increase Speed
RewriteEngine On
# Set the cache-control max-age
# 1 year
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=31449600, public"
</FilesMatch>
# 2 DAYS
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>
# 4 HOURS
<FilesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=14400, must-revalidate"
</FilesMatch>
Block Users By IP
Order allow, deny Deny from 192.168.0.10 Deny from 212.155. Allow from all
Set Custom Error Pages
RewriteEngine On ErrorDocument 500 /internal_error.html ErrorDocument 401 /authorization_required.html ErrorDocument 403 /forbidden.html ErrorDocument 404 /not_found.html
Prevent Image Hotlinking
RewriteEngine On RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]
Tagged as: htaccess, mod rewrite











