.htaccess subdomain redirection to root domain

I just want to ask a quick question about .htaccess.

This is how my web hosting works with subdomains ...

As soon as I create a subdomain ... then they create a folder in a root folder like this ...

www.mydomain.com ---> public_html
sub.mydomain.com ---> public_html/sub

What I want to do is ... redirect the entire request from sub.mydomain.com to www.mydomain.com with some GET variable or something to determine which subdomain the request comes from ...

So for example ... when I get job requests like this

http://sub.mydomain.com/myphp.php ---> http://www.mydomain.com/myphp.php?comingfrom=sub
http://sub.mydomain.com/(anyUrl)  ---> http://www.mydomain.com/(anyUrl)?comingfrom=sub

I am also wondering if this will do any .htaccess redirects present in the main domain ...

I hope you guys could help me ...

Thanks in advance...

+3
source share
1 answer

.htaccess :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) http://www.mydomain.com$1?comingfrom=%1 [QSA,R=301,L]

(, )

EDIT:

, . , <VirtualHost> www.mydomain.com

 <VirtualHost ...:80>
   ServerName www.mydomain.com
   ServerAlias mydomain.com
   ServerAlias sub.mydomain.com

   DocumentRoot /path/to/your/docroot/of/www.mydomain.com

   RewriteEngine On
   RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
   RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
   RewriteRule ^(.*) $1?comingfrom=%1 [QSA,PT,L]
 </VirtualHost>
+1

All Articles