0

My issue is simple but I can't seem to find the solution.

I have a domain www.domain.com that has subdomains.

www.domain.com 's document root is /home/domain/public_html

Subdomains (excluding www) need to be redirected to example.com i.e. abc.domain com need to be redirected to /home/example/public_html

Also, the url needs to be retained. i.e. abc.domain.com need to show in the address bar.

What I have done till now I have maintained in httpd.conf the following entry under

  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^(.*)domain.com$
  RewriteRule ^.*$ http://%1example.com$0

However, the url is changing to abc.example.com and www.domain.com is being redirected to www.example.com too.

Please suggest the best way to resolve this.

Virtual Hosts configuration, without Rewrite rules,

  <VirtualHost *:80>
      ServerName domain.com
      ServerAlias *.domain.com www.domain.com
      DocumentRoot /home/domain/public_html
      UseCanonicalName Off
      ## User abc # Needed for Cpanel::ApacheConf
      UserDir enabled abc
      <IfModule !mod_disable_suexec.c>
          <IfModule !mod_ruid2.c>
             SuexecUserGroup abc abc
          </IfModule>
      </IfModule>
      <IfModule mod_ruid2.c>
          RMode config
          RUidGid abc abc
      </IfModule>
      <IfModule itk.c>
          AssignUserID abc abc
      </IfModule>
  </VirtualHost>
  • I assume you meant `/home/domain/public_html` in both cases in your question, and not `/home/example/public_html` for the subdomain. – Tero Kilkanen May 29 '16 at 19:59
  • No I meant /home/example/public_html for the subdomain. Subdomains need to be redirected & masked too. – user2739655 May 30 '16 at 06:26
  • I think you are using confusing terms here, which makes it hard to understand your goal. Redirect is when user's browser is instructed to load another URL, for example, loading `http://www.example.com/test`, the server sends a `301` redirect telling browser to load `http://www.example.com/another` URL instead. Another thing is changing the document root for a domain on the webserver. Please try to re-phrase your question so that your goal is more clearly stated. Please include also your virtual host configuration from your webserver. – Tero Kilkanen May 30 '16 at 08:09
  • Thanks. It's my lack of knowledge on the correct terminology that's confusing. Let me try again. I want the document root of **www.domain.com** to be `/home/domain/public_html`. For any other subdomain (e.g. **abc.domain.com**), the document root should be `/home/example/public_html`. Also, **the URL should be retained** i.e. abc.domain.com should not get converted to abc.example.com. – user2739655 May 30 '16 at 08:18
  • And the virtual host(s) configuration? – Tero Kilkanen May 30 '16 at 08:21
  • Included the configuration in the main question. – user2739655 May 30 '16 at 08:27
  • I updated my answer based on the information. – Tero Kilkanen May 30 '16 at 09:03

2 Answers2

0

This worked for me:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^w]*).domain.com$
RewriteRule ^.*$ http://%1.example.com$0

The regular expression will now match any hostname other than a string of w's. (This would need to be modified if you ever needed w.domain.com, ww.domain.com, wwww.domain.com, etc. to work.)

cherdt
  • 405
  • 2
  • 4
  • 13
  • Thanks for the www exclusion rule. But the subdomain redirection masking is not working. abc.domain.com is getting transformed to abc.example.com. – user2739655 May 29 '16 at 15:49
0

You don't need a RewriteRule directive here. You need to make a virtual host for the subdomain, and then assign the same document root as the main domain to that subdomain.

RewriteRule is used when you need to redirect the users' browsers to new URLs, and the new URL always shows up in browsers' URL field.

So, you should remove the RewriteRule and related directives, and use this virtual host configuration:

<VirtualHost *:80>
  ServerName domain.com
  ServerAlias www.domain.com
  DocumentRoot /home/domain/public_html
  UseCanonicalName Off
  ## User abc # Needed for Cpanel::ApacheConf
  UserDir enabled abc
  <IfModule !mod_disable_suexec.c>
      <IfModule !mod_ruid2.c>
         SuexecUserGroup abc abc
      </IfModule>
  </IfModule>
  <IfModule mod_ruid2.c>
      RMode config
      RUidGid abc abc
  </IfModule>
  <IfModule itk.c>
      AssignUserID abc abc
  </IfModule>
</VirtualHost>

<VirtualHost *:80>
  ServerName other.domain.com
  ServerAlias *.domain.com
  DocumentRoot /home/example/public_html
  UseCanonicalName Off
  ## User abc # Needed for Cpanel::ApacheConf
  UserDir enabled abc
  <IfModule !mod_disable_suexec.c>
      <IfModule !mod_ruid2.c>
         SuexecUserGroup abc abc
      </IfModule>
  </IfModule>
  <IfModule mod_ruid2.c>
      RMode config
      RUidGid abc abc
  </IfModule>
  <IfModule itk.c>
      AssignUserID abc abc
  </IfModule>
</VirtualHost>

For the second virtual host, you can use any non-existing sub-domain name for the ServerName directive. However, you need a non-wildcard ServerName directive there, since wildcards are allowed only in ServerAlias directive.

The trick here is that the first VirtualHost block is used for domain.com and www.domain.com, since Apache uses the first matching VirtualHost block. There the document root is set to /home/domain/public_html.

The second VirtualHost block is used for all other matching domains and there the document root is /home/example/public_html.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Worked like a charm. Can't return your great favour but will surely pay it forward :) – user2739655 May 30 '16 at 13:09
  • Oh ok. What to do with simply **domain.com** that should be treated as www.domain.com too? Currently with this virtual host configuration it's being directed to example.com. – user2739655 May 30 '16 at 18:19
  • Maybe you need to add `domain.com` to the `ServerAlias` line in the first virtual host configuration. – Tero Kilkanen May 30 '16 at 22:07