Automatically redirect requests for root domain to www subdomain on all new sites

DanH42

Active Member
Sep 11, 2011
35
0
56
Bloomington, IL
cPanel Access Level
Root Administrator
I'd like to have all new domains include a rule, either in a VirtualHost or a .htaccess, that redirects requests to the root domain (example.com) to a subdomain (www.example.com). Currently, I'm doing this manually by putting a .htaccess file in everyone's home folders like this:
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This is starting to get tedious to do for every account, since (in my case) almost all clients want the functionality.

Is there a way to configure WHM do do something like this for every new domain when it's created? I don't much care how it's achieved, but it's important that I still be able to disable it on a per-site basis if requested.
 

DanH42

Active Member
Sep 11, 2011
35
0
56
Bloomington, IL
cPanel Access Level
Root Administrator
Thanks, that looks useful.

The changes I need to make will have to reference the name of the domain within themselves. Is there some variable I can use that would represent the current domain?

Alternatively, is there a different way to do that redirection that would be domain-agnostic?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,260
463
You can review the template files for the version of Apache installed on your system via:

/var/cpanel/templates

This should give you a better idea on how to reference a domain name, and is likely the better route compared to attempting to formulate a non-domain specific Mod_Rewrite rule.

Thank you.