About Linux FAQ

Browse More FAQs:

Advertisements

Apache redirect domain.com to www.domain.com

Posted by Vivek Gite [Last updated: June 26, 2007]

Q. What's the best way to redirect domain.com to www.domain.com on the url address bar whenever someone omits or skip the www from domain.com? My site can be reached by two different URLs. For example http://domain.com/about/us.html and http://www.domain.com/about/us.html

How do I do configure Apache web sever vps for this task?

A. Apache (web server) 301 redirect is the most efficient and search engine friendly method for webpage redirection. You can place following code Apache’s httpd.conf vhost section or in .htaccess file.

$ vi .htaccess
Append following config code:

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

Save and close the file. Above code will redirect users to www.domain.com url.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Local Service Quotes Says:

    Thanks for the info and great site!

  2. luca Says:

    There actually is a little correction to be made.
    With the code you wrote, if you enter the site
    http://domain.com/index.php
    you are redirected to
    http://www.domain.com//index.php
    Notice the double slashes.

    The correction you need to do is on this line:

    RewriteRule ^(.*)$ http://www.domain.com1 [R=permanent,L]

    just remove the slash before $1

  3. S. Nilesh Says:

    If you have mod_alias enabled, then no need for Rewrites.

    Use this to redirect requests to domain.com to http://www.domain.com

    RedirectPermanent / http://www.domain.com

    This will Redirect http://domain.com/anything/foo/bar/baz to http://www.domain.com/anything/foo/bar/baz

    mod_alias is available in Apache 1.3 also.

  4. S. Nilesh Says:

    Use mod_alias’s RedirectPermanent directive.

    RedirectPermanent / “http://www.domain.com/”

    it will redirect

    domain.com/foo/bar/baz to http://www.domain.com/foo/bar/baz

    mod_alias is available in Apache 1.3 also

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.