htaccess redirect

How to use htaccess file

The .htaccess file is a system configuration file which generally is located in the same location as your index file. This website will explain how to create a .htaccess file to redirect your site or site content. Please not that .htaccess configuration will not redirect any emails for your domains. If you wish to redirect e-mail you will need to change the DNS settings of your domain.

.htaccess is a very powerful file. Even the slightest syntax error (like a missing space) can result in your content not displaying correctly or at all. Please play responsibily. Always take backups.

Any line with a # (hash, number) sign in the beginning is a comment. It will have no affect on your configuration.

redirect entire website permanently

This allows you to redirect your entire website to any other domain permanently.

Redirect 301 / http://example.com/

If you wish to redirect your files parmenently you can do it like so:

Redirect 301 /file.html http://example.com/newfile.html

redirect entire website temporarily

This allows you to redirect your entire website to any other domain temporarily.

Redirect 302 / http://example.com/

redirect index.html to a specific subfolder

This allows you to redirect your index.html file to a specific subfolder.

Redirect /index.html http://example.com/newdirectory/

redirect old file path to new file path

Use this redirection if your files or paths have changed. By doing this you will make sure that your traffic is not lost in your 404 pages. If you have purchased an expired domain that already has some pages indexed within search engines, this would be a great way of redirecing the traffic to your newly created pages.

Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html

redirect to a specific index

Use this to provide Specific Index Page of your website (Set the default handler)

DirectoryIndex index.html

htaccess rewrite

redirect/rewrite www to non-www OR non-www to www

Google can treat a www and non-www URL as totally different pages. Make sure that your htaccess file has only one of those. If you have Google Webmaster Tools (GWT) setup for your domain, please make sure that this rewrite matches the setup of your domain in GWT.

www to non-www

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

non-www to www

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

protect your WordPress login page

If you have a self-hosted version of Wordpress it is very important to protect your login page. You can do this by allowing only certain IPs to access this directory. Create a .htaccess file and place it in your wp-admin directory:

# Please change 0.0.0.0 with your own IP(s)
# For your information, your IP is 54.234.180.187

order deny,allow
deny from all
# whitelist home IP address
allow from 0.0.0.0
# whitelist work IP address
allow from 0.0.0.0
# IP while away, allow multiple IPs
allow from 0.0.0.0
allow from 0.0.0.0