Apache redirect domain.com to www.domain.com
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:
- Howto rebuild Apache for Cpanel Linux / UNIX Server (Control Panel)
- Apache prevent hot linking or leeching of images using mod_rewrite howto
- PHP Redirect To Another URL / Page Script
- Printing output of c program to a file in Linux
- How do I find out syntax errors in my Apache web server configuration file?
Discussion on This FAQ
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!
Tags: apache_web_server, htaccess_file, httpd, Linux, UNIX, vps




July 9th, 2007 at 6:05 am
Thanks for the info and great site!
August 1st, 2008 (5 weeks ago) at 7:19 am
There actually is a little correction to be made.
With the code you wrote, if you enter the site
http://domain.com/index.phpyou are redirected to
http://www.domain.com//index.phpNotice 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
August 1st, 2008 (5 weeks ago) at 4:11 pm
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.
August 1st, 2008 (5 weeks ago) at 4:27 pm
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