1

I use this config in apache2 for wildcard subdomain and works well:

 <VirtualHost _default_:80>
   UseCanonicalName off
   Options FollowSymLinks
   VirtualDocumentRoot /var/www/domains/%0/httpdocs

    RewriteEngine On

    RewriteCond   %{HTTP_HOST}      ^\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
    RewriteRule   ^(.+)             /var/www/domains/localhost/httpdocs/$1 [L]

    RewriteCond   %{HTTP_HOST}      !^[^.]+\.abc\.net [NC]
    RewriteCond   %{HTTP_HOST}      !^[^.]+\.example\.me [NC]
    RewriteCond   %{HTTP_HOST}      !^[^.]+\.mysite\.info [NC]
    RewriteCond   %{HTTP_HOST}      !^[^.]+\.xyz\.org [NC]
    RewriteCond   %{HTTP_HOST}      !\.internal$ [NC]
    RewriteCond   %{REQUEST_URI}    !^/collateral/ [NC]

    RewriteRule   ^(.+)             /var/www/domains/%{HTTP_HOST}/httpdocs/%{HTTP_HOST}/$1
    ErrorDocument 403 /error/404.php

This means a request for "http://sub.abc.net" will fetch the documentroot "/var/www/domains/sub.abc.net/httpdocs" & request for "http://sub.xyz.org" will fetch the documentroot "/var/www/domains/sub.xyz.org/httpdocs".

But I want the request to *.abc.net would be rewritten to *.xyz.org documentroot.

  • Can you clarify your example hostnames. The code you posted specifically excludes hostnames of the form `sub.abc.net` and `sub.xyz.org`, so it's not _that_ code that is doing this rewrite, as you appear to suggest? Also, that code rewrites to a subdirectory of the document root, not just the document root, as you state? Do you only have one `.net` domain? Can the solution be specific to that domain, or do you need an any `.net` domain solution? – MrWhite Feb 28 '18 at 14:58
  • 1
    Hi MrWhite, Thanks for your reply. We need any .org domain should redirect to .net domain documentroot – Pankaj Gupta Feb 28 '18 at 19:45

1 Answers1

0

We need any .org domain should redirect to .net domain documentroot

I assume you mean "rewrite", as opposed to external "redirect", like the code in your question? (You've also reversed the direction of the required rewrite, as stated in your question, but anyway, I'll go with what you stated in comments... .org to `.net)

This assumes that you only have single level TLDs, as referenced in your code (eg. .net, .me, .info, .org, etc) and not multi-level "TLDs" such as .co.uk.

RewriteCond   %{HTTP_HOST}      !^[^.]+\.abc\.net [NC]
RewriteCond   %{HTTP_HOST}      !^[^.]+\.example\.me [NC]
RewriteCond   %{HTTP_HOST}      !^[^.]+\.mysite\.info [NC]
RewriteCond   %{HTTP_HOST}      !^[^.]+\.xyz\.org [NC]
RewriteCond   %{HTTP_HOST}      !\.internal$ [NC]
RewriteCond   %{REQUEST_URI}    !^/collateral/ [NC]
RewriteRule   ^(.+)             /var/www/domains/%{HTTP_HOST}/httpdocs/%{HTTP_HOST}/$1

Try something like the following instead:

# If any of these domains are requested then stop
RewriteCond %{HTTP_HOST}      ^[^.]+\.abc\.net [NC,OR]
RewriteCond %{HTTP_HOST}      ^[^.]+\.example\.me [NC,OR]
RewriteCond %{HTTP_HOST}      ^[^.]+\.mysite\.info [NC,OR]
RewriteCond %{HTTP_HOST}      ^[^.]+\.xyz\.org [NC,OR]
RewriteCond %{HTTP_HOST}      \.internal$ [NC,OR]
RewriteCond %{REQUEST_URI}    ^/collateral/ [NC]
RewriteRule ^ - [L]

# Split the requested host into HOSTNAME + TLD (env vars)
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)\.([^.]+)
RewriteRule ^ - [E=HOSTNAME:%1,E=TLD:%2]

# Change TLD "org" to "net"
RewriteCond %{ENV:TLD} =org [NC]
RewriteRule ^ - [E=TLD:net]

RewriteRule (.+) /var/www/domains/%{ENV:HOSTNAME}.%{ENV:TLD}/httpdocs/%{ENV:HOSTNAME}.%{ENV:TLD}/$1 [L]

The first block of directives reverses your current conditions. ie. It says if the request is for any of "these domains" then stop processing, instead of... only rewrite when the request does not match any of "these domains". This makes the following rules simpler, since we now know that the requested host is one that we want to rewrite, so we don't need to litter the code with more conditions.

We then split the requested HTTP_HOST into the hostname (subdomain + domain) and the tld only. And store these in two environment variables: HOSTNAME and TLD respectively.

We then test the TLD env var and change this if necessary (ie. org to net).

And lastly, there is an unconditional rewrite to the appropriate document root and subdirectory (using the env vars assigned above), as in your code. Note that you stated just "document root" in your description, but your code also includes a subdirectory of the same name, so I've included the subdirectory as well.

This assumes the mod_rewrite directives stated in your question are complete. ie. There's no other dependent directives further down the config? I added the L flag for good measure.

MrWhite
  • 12,647
  • 4
  • 29
  • 41