handy .htaccess settings I use in a row



I always keep on reinventing the wheel when it comes to .htaccess settings like for automatic redirect to https://, utf-8 character settings or custom error documents. Here are some settings I use.
Automatic redirect to ssl host https:
In case you want to automatically redirect to https to make sure people use the secure connection put the following code in your .htaccess.
Always make sure to use %{HTTPS} off as condition and not any port number.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Custom "page not found" or other error pages
ErrorDocument 404 http://errorurl/
//or the following
ErrorDocument 404 /edocs/404.htm
Make sure host uses UTF-8
if you create websites for others and use a lot of different hosting providers you may notice not everyone uses utf-8. This might create some difficulties with certain characters. Adding the following line to your .htaccess makes sure your host uses utf-8.
AddDefaultCharset utf-8
Code I always put on top to prevent problems with some hosts
-Indexes makes sure your folders aren't indexable from the web. This might prevent some unwanted peeking around on your server colocation.
Options -Indexes +SymLinksIfOwnerMatch
April 25th, 2008 at 11:23 am
To redirect users entering the site at http://domainname.com to http://www.domainname.com you can use the following .htaccess rewrite rules:
RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
Add A Comment