Support Center » Knowledgebase » Unix Tips » How to redirect from domain.com to www.domain.com

How to redirect from domain.com to www.domain.com



Unix Hosting

With Apache's mod_rewrite module, you can do some fancy things by manipulating the url.

On Quadra Hosting, your site can be accessed by either http://domain.com and http://www.domain.com

This is a handy feature as you don't have to mention the "www" bit when quoting your web site, sometimes you need to make sure everyone is accessing your site with the "www" bit.

One of the things you can do with mod_rewrite is to redirect domain.com to www.domain.com, so that when someone types domain.com, they will be forwarded to www.domain.com automatically.

This is especially handy if your site keeps a persistent cookie since it's normally based on the domain name.

The rule to do this is quite simple too:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*)
http://www.%{HTTP_HOST}/$1 [L,R]

These lines need to go into a file called .htaccess which resides in your web root directory. If you don't already have one, just create it. It is a simple text file.


To achieve the reverse (that is to redirect www.domain.com to domain.com) use this rule instead:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www
RewriteRule (.*)
http://domain.com/$1 [L,R]

Note that you would need to specify the actual domain.com in the above case. If you know how to do this using a variable (without hard coding the domain name) please let us know.

Windows Hosting

Users on Windows 2008 (IIS7) are able to rewrite URL's using the URL Rewrite Module. This method is similar to using Apache, but your rules will go in <system.webServer> section of your web.config file. Here is an example Rule that will rewrite non-WWW URL's to WWW:

<rewrite>
    <rules>
        <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="domain.com" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>




Article Details
Article ID: 100
Created On: 25 Feb 2006 12:53 PM
 Back
 Login [Lost Password] 
Email:
Password:
Remember Me:
Please note that the login and password to the support area is NOT the same as your hosting control panel login and password
 
 Search
 Article Options
Home | Register | Submit a Ticket | Knowledgebase | Downloads | Control Panel User's Guide | Server Status
Language: