Apache redirect www.domain.com to domain.com

Q. How do I remove www from all my urls so that search engine can only rank domain.com/ urls under Linux Apache web server?


A. Add following code to your .htaccess or httpd.conf (directory directive) file:

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

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

If you added directives to httpd.conf, restart Apache:
# service httpd restart

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.