Archive

Posts Tagged ‘301 Redirect’

www, non-www and subdomains

March 30th, 2009

If you want to redirect non www users to www version of your website, you can use the following code:

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

but in case of sub domains, this’ll create problems. The code above redirects anything *except* www.example.com to www.example.com.

You’d undoubtedly be happier using a positive-match pattern, instead of the negative match used in the above example.

RewriteEngine On
#
# Redirect www.<subdomain>.example.com/<anything> to <subdomain>.example.com/<anything>
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#
# Redirect example.com/<anything> to www.example.com/<anything>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Cheers & Happy Coding….

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit This Post Post to StumbleUpon Stumble This Post

Tips & Tricks , ,

Redirect Visitors From Non-WWW To WWW With .htaccess And mod_rewrite

January 26th, 2009

Apache’s mod_rewrite module is a very robust and effective tool and can be used for lots of important stuff related to your website. One of it’s application is to force visitors and search engine robots to either the WWW form of your website (i.e. www.yourdomain.com), or the non-WWW form of your website ( i.e. yourdomain.com).

To accomplish the desired objective you need to add just three lines of code to your .htaccess file. For those of you that don’t know, an .htaccess file is a simple ASCII file that you can create in a text editor like Notepad. The actual name—or rather, the extension—of the file is .htaccess. It’s not file.htaccess, or index.htaccess, or anything else, it’s simply named .htaccess.

Look for it in your site’s root folder/directory (which in most cases would be ‘pubic_html’ or ‘www’ – It does not matter which directory you choose, as they are both the same thing. ). If it’s not there simply use a text editor, insert the code written below, save and upload to your website root. You are now all set to send users typing your website address without www (i.e. yourdomain.com) to the WWW version of your website. Here is the code:

RewriteEngine On

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

Easy, isn’t? Now to send visitors to the non-WWW version of your website. The following lines of code would automatically redirect users typing your domain name with www (i.e. www.yourdomain.com) to the non-WWW version:

RewriteEngine On

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

But this is something you should think about at the earlier stages (design and development) of your web site. Applying this after you’ve already been indexed may result in loss of PageRank or even a complete removal and then re-index of your website by the search engine bots.

Hope this’ll help… cheers

Please click below for an updated post especially with regards to subdomains.

http://www.ranatariq.com/tips-tricks/www-non-www-and-subdomains/

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit This Post Post to StumbleUpon Stumble This Post

Knowledgebase, Tips & Tricks , , ,

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.