Home > On-site Optimization, SEO Tutorial Canada, Web Development and Design Services > The importance of .htaccess | Using .htaccess Rewrite Rules | .htaccess Example

The importance of .htaccess | Using .htaccess Rewrite Rules | .htaccess Example

The importance of .htaccess | Using .htaccess Rewrite Rules | .htaccess Example

.htaccess Rewrite Rules

Hypertext Access, commonly abbreviated to .htaccess is a configuration file for use on web servers like Apache Server. It is used to control the directory it is placed in and all the subdirectories under it. This file indicates which users of groups of users can be allowed to access to the files which are placed in that directory. Htaccess file has the capability to restrict access to some folders or subfolders on the internet through its password protection. This file also enables to block or allow certain IP address access. It commands over the server and guide the server as how to react and behave in certain situations. This file is created in notepad or text editor, after creation it is placed in the Root Directory of the Web Server.

Through .htaccess we can:

  1. Password Protection: If we want a specific area or folder to be protected and want only certain people can access it then with .htaccess file you are safely protected.
  2. 301 Redirect: By redirection we refer to direct visitor from one web page to another within the website. Itis considered the most effective and search friendly way to redirect pages, URLs, etc. If we have moved our webpage content then with .htaccess file we can redirect users from old links to new webpage content links.
  3. Block Users: We can block various bots with .htaccess file like we can keep spammers, users not to access the certain area of the website. This is used to avoid unwanted visitors to access the website area where only specific people can have access to the website.
  4. Custom 404 Pages: Through customized error page creation we can make visitor provided with the search friendly message instead of “not found” error message. Here we can define error pages to maintain communication with the visitor.

Properties of .htaccess

  • An .htaccess file is a hidden file like any other beginning with a dot. They are called dot files.
  • It must be a plain text file created in notepad or text editor.
  • An .htaccess applies server directories to a directory and all subdirectories.
  • An .htaccess is not the name of the file, it is a file extension. It is simply called .htaccess.

Example – 301 Redirects in Apache .htaccess

1. Redirect WWW to non-WWW:

RewriteEngine On

RewriteCond %{ HTTP_HOST} ^example\.com$ [NC]

RewriteRule ^(.*)$ http://www. example.com/$1 [R=301,L]

2. Redirect non-WWW to WWW:

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

3. Redirect index.php to WWW:

RewriteCond %{THE_REQUEST} “/index.php HTTP/1.1”
RewriteRule index\.php http://www.unlockingcell.com/ [R=301,L]

Example – 301 Redirects in IIS .htaccess

With IIS7, there are actually two ways to do this. The URL Rewrite extension is required for this.

1. Redirect WWW to non-WWW:

<rule name=”www to non www”” enabled=”true”>
url=”(.*)” />
<add input=”{HTTP_HOST}” negate=”true” pattern=”^www\.example\.com$” />
url=http://www\.example\.com/{R:1}” redirectType=”Permanent” />

2. Redirect non-WWW to WWW:

<rule name=”non www to www” enabled=”true”>
url=”(.*)” />
<add input=”{HTTP_HOST}” negate=”true” pattern=”^www\.example\.com$” />
<action type=”Redirect” url=”http://www\.example.\com/{R:0}” redirectType=”Permanent” />

Example – Https enabled site with seo issues

“Then add the following lines to your .htaccess (in the root of your webhosting).

 RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots_ssl.txt

Example – Create a 301 redirect for all http requests that are going to the old domain

1. Redirect from oldexample.com to http://www.newexample.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]

This is useful when you use http://www.newexample.com as your new domain name (see also this article about redirecting www and non-www domains). If not – use the code of example 2

2. Redirect from oldexample.com to newexample.com:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301]

Quick, easy-to-understand tips for .Htaccess

Symbols Why used
^example\.com$ It is the Regular expression  to which we compare the referrer
“^” It means string start
“\” used to escape the dots (which otherwise are part of Regular expression syntax
“$” Means string end.
[L] flag causes mod_rewrite to stop processing the rule set
  • [NC] means we don’t care about the case.
  • RewriteCond %{ HTTP_HOST} ^example\.com$ [NC]: RewriteCond is a directive which defines a condition under which rewriting will take place.
  • The syntax is RewriteCond $StringToTest $Regular expression [$flags].
  • RewriteRule is a directive which defines rules for the rewriting engine.
  • The syntax is RewriteRule $ Regular expression $Substitution [$flags].
  • Read More about Block or remove pages using a robots.txt file
  1. Daniele
    July 18, 2012 at 3:47 pm

    This article is really a great help especially to a newbie like me. great one.

  2. monika
    September 3, 2015 at 12:31 pm

    how to sub.domain work on different ip address

  1. April 15, 2014 at 4:52 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: