3

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

Here's how my webhosting works with subdomains...

Once i create a subdomain... then they create a folder into the root folder like this...

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

What i want to do is... to redirect all request from sub.mydomain.com to www.mydomain.com with some GET variable or something to identify from what subdomain the request is coming from...

So for example... when i get a requests to work 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'm also wondering if this would execute some .htaccess redirects present in the main domain...

Hope you guys could help me...

Thanks in advance...

saggio09
  • 177
  • 1
  • 2
  • 10

1 Answers1

1

Put a .htaccess with following content into your subdomain folders:

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

(untested, sorry)

EDIT:

you pointed out that you would like to keep the subdomain in your addressbar and don't want a redirect. So you need to make a view changes to your <VirtualHost> of www.mydomain.com like so

 <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>
Seybsen
  • 14,989
  • 4
  • 40
  • 73
  • 1
    This works... but it "changes" the url... any way to keep it like sub.mydomain.com and internally execute the redirected page? – saggio09 Apr 28 '12 at 13:53
  • Though, i didn't use this solution because my hosting doesn't allow it... it's the solution for the problem... So i'm gonna put this as the solution of my problem... – saggio09 May 02 '12 at 17:18