The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'General Discussion' started by DanH42, Jun 5, 2014.

  1. DanH42

    DanH42 Active Member

    Joined:
    Sep 11, 2011
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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.
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  3. DanH42

    DanH42 Active Member

    Joined:
    Sep 11, 2011
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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?
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
Loading...

Share This Page