.htaccess

.htaccess, or hypertext access, is a file that allows directory-level configuration changes.

Changes by this file apply to the directory where it is located and all subdirectories. It does not have a file name, only a file extension. The leading dot designates it as a hidden file.

Instructions

  1. Open Notepad
  2. Copy and paste code below
  3. Click File
  4. Click Save As...
  5. After File name:, type .htaccess
  6. After Save as type:, select All Files
  7. Click Save
  8. Upload .htaccess to root directory of your website

non-www

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^w+\.(domain\.com)$ [NC]
RewriteRule (.*)         http://%1/$1        [R=301,L]
From To
www.domain.com domain.com

www

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(domain\.com)$  [NC]
RewriteRule (.*)         http://www.%1/$1 [R=301,L]
From To
domain.com www.domain.com

Key

Options         - options
+FollowSymLinks - follow symbolic links

RewriteEngine - rewrite engine
on            - on

RewriteCond - rewrite condition
RewriteRule - rewrite rule

%{HTTP_HOST} - host

^ - start of string
$ - end   of string

() - group

%N - backreference to rewrite condition group (1<=N<=9)
$N - backreference to rewrite rule      group (0<=N<=9)

. - any character
? - matches previous character zero or one  time
* - matches previous character zero or more times
+ - matches previous character one  or more times

\ - escape (treat special characters as normal characters)

[L]     - last
[NC]    - no case (case insensitive)
[R]     - temporary redirect
[R=301] - permanent redirect

External

Apache .htaccess

Apache Options

Apache Regular Expressions

Apache Rewrite Condition

Apache Rewrite Engine

Apache Rewrite Rule