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.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.