unlimited domain web hosting
Discussion forums relating to Web Design and Development
Redirecting to www prefix

Post new topic   Reply to topic    Web Design and Development Community Forum Index -> Coding Help
View previous topic :: View next topic  
Author Message
Site Admin

Joined: 17 Mar 2006
Posts: 58
Location: England

PostPosted: Wed Mar 22, 2006 2:30 pm    Post subject: Redirecting to www prefix Reply with quote

Its a common request - you want visitors to your web site to all be sent to the URL with the www prefix. For example, instead of allowng them to simply use yourdomain.com, you want to force them to www.yourdomain.com (where yourdomain.com is of-course replaced with your actual domain name).

We can do this with a mod_rewrite directive in your htaccess file.

Simply add the following lines to your .htaccess file which is stored in your web folder (called public_html) ...


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

An explanation of what this does ...

RewriteEngine on ~ this initiates a mod_rewrite directive
RewriteCond %{HTTP_HOST} ^([^.]+\.com) [NC] ~ this line checks to make sure the request is not a www or subdomain request before proceeding with the next line
RewriteRule (.*) http://www.%1/$1 [R=301,L] ~ this simply redirects the request to the corresponding web page but with the www

As indicated, this will redirect all requests for any page within that domain to the same page but with the added www in the URL.

Reasons for doing this? The main reason for its use is to focus search engines and the corresponding page rank into one place, since search engines generally depict yourdomain.com/anypage.html and www .yourdomain.com/anypage.html as two different pages and you will subsequently see alternative page ranks or search engine results for either of them.


Multiple Domain Hosting Plans ~ Domain Name Registration ~ Support
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   All times are GMT
Page 1 of 1

Jump to: