handy .htaccess settings I use in a row

Submitted by Tim on 16 Apr 2008 - 22:14

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

Comments

user picture>
- 25 Apr 2008 - 11:23

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 new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Tags