Search code examples
apache.htaccessmod-rewriteshared-hostingvirtual-server

www redirect to non www for all domain hosted


I am hosting sever sites by using virtual host of apache. Is there any way to redirect any sites hosted on our server to will redirect to non www

I want just one setting in the server so that every site hosted here and that will be hosted later will redirect to non www if there is a www in the request. I dnt like the .htaccess change for all the sites individually.


Solution

  • Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    

    Just one rule like this will take care of removing www from all of your hosts.