You Are Here: Home » Articles » How-To » Programming » Tech » Tips & Tricks » Web

Redirect visitors to domain.com from www.domain.com and vice-versa in Apache

By Debjit on June 20th, 2009 
Advertisement

apache logoIn order to get  maximum Google juice to your website, you should make sure that all your content point to one universal URL. For eg. If you use www.yourdomain.com as your website address then make sure that you set this as the default hyperlink to every file on your server. Then do not use domain.com for linking files and content on your server.

And if you use domain.com as your default URL then do not use www.domain.com. If this is not the case, then you can use a 301 redirect to fix the situation. A 301 Redirect tells that an URL has permanently moved to a new location. In Apache, there is a provision for the .htaccess file which helps you make configuration changes on a per-directory basis. We will use this .htaccess file to setup a 301 redirect for an Apache server. In this article we will tell you everything about how to setup a 301 redirect.

For a 301 redirect from http://www.yourdomain.com to http://yourdomain.com

  • Create a .htaccess file in your DocumentRoot (www or public_html) directory and paste the following lines into that file. If the .htaccess file already exists then just append the following lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]

For a 301 redirect  from http://yourdomain.com to http://www.yourdomain.com

  • Create a .htaccess file in your DocumentRoot (www or public_html) directory and paste the following lines into that file. If the .htaccess file already exists then just append the following lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

If you have any problems setting up a 301 redirect for your website, then let us know about it either in the comments below, or via email: mailman [at] digitizor [dot] com

Advertisement







Redirect visitors to domain.com from www.domain.com and vice-versa in Apache was originally published on Digitizor.com on June 17, 2009 - 11:51 am (Indian Standard Time)