Apache redirect all traffic to www. and vice versa in .htaccess

Hi Guys,

So I had this problem few weeks back and would like to share the solution with you. My dilemma was to redirect all traffic from http://www.jaspreetchahal.org to  http://jaspreetchahal.org

If you like the look and feel of this website then you must look at Genesis Framework.
Click here to find about Genesis Framework
This is strongly recommended by me.

There were few reasons why I wanted it to be like that. One of them was the Google Analytics stuff. GA works on per domain name basis (Correct me if I am wrong here) so tracking for jaspreetchahal.org is not same as for www.jaspreetchahal.org.

Now I had a choice to either put the tracking on for both www.jaspreetchahal.org and plain jaspreetchahal.org. I am glad that the solution was this simple.

I strongly recommend this book on mod_rewrite for you

We just need to edit our .htaccess file in the root directory of your website.

Three steps to make sure that all will work correctly

  • Make sure that you Apache mod_rewrite module is available and is loaded
  • Make sure that under your website directory options you have AllowOverride option set to all i.e.
    <director “path/to/your/website/directory”>
    AllowOrderride All
    </directory>
  • Third is to make this change in your .htaccess file
    For redirecting everything from yourdomain.com to www.yourdomain.com do this
    1. # turn on the rewrite engineRewriteEngine On
    2. #condition: if domain name starts with yourwebsite.com
    3. RewriteCond %{HTTP_HOST} ^yourwebsite.com$ [NC]
    4. # apply this rule: redirect the request to include www. as prefix
    5. RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [R=301,L] R=301 tells that its a Redirect and 301 tells that its a permanent redirect. L tells that its the last rule
    For redirection everything from www.yourdomain.com to yourdomain.com do this
    1. # turn on the rewrite engine
    2. RewriteEngine On
    3. #condition: if domain name starts with yourwebsite.com
    4. RewriteCond %{HTTP_HOST} ^www.yourwebsite.com$ [NC]
    5. # apply this rule: redirect the request to exclude www. as prefix.
    6. RewriteRule ^(.*)$ http://yourwebsite.com/$1 [R=301,L] R=301 tells that its a Redirect and 301 tells that its a permanent redirect. L tells that its the last rule

    Another thing I would like to add here is that If you are not sure if mod_rewrite is available or not then its always a good idea to wrap your Rewrite code with <Ifmodule> tag as shown below

    The above lines thus can be rewritten as

    1. <IfModule mod_rewrite.c>
    2. RewriteEngine On
    3. #condition: if domain name starts with yourwebsite.com
    4. RewriteCond %{HTTP_HOST} ^www.yourwebsite.com$ [NC]
    5. # apply this rule: redirect the request to exclude www. as prefix.
    6. RewriteRule ^(.*)$ http://yourwebsite.com/$1 [R=301,L] R=301 tells that its a Redirect and 301 tells that its a permanent redirect. L tells that its the last rule
    7. </IfModule>

After you made the above change then based on the rule you’ve used your website visitors will be directed accordingly.



VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Speak Your Mind

*

CommentLuv badge