1

I am needing to translate an unknown subdomain to a variable so the following would apply:

  • http://test.example.com/http://www.example.com/?domain=test

  • http://xyz.example.com/http://www.example.com/?domain=xyz

  • http://www.example.com/http://www.example.com/

  • http://www.example.com/pageA/http://www.example.com/pageA/

  • http://fish.example.com/pageB?somevar=somethinghttp://www.example.com/pageB?somevar=something&domain=fish

  • http://www.example.com/pageB?somevar=somethinghttp://www.example.com/pageB?somevar=something

  • http://fish.example.com/pageBhttp://www.example.com/pageB?domain=fish

As you can see all I need to do is replace any subdomain with www and append the subdomain name as a get var called domain.

I'm getting really lost with this.

Edit: Oh I also want the user to still see the subdomain in the url, rather than redirecting.

DwayneBull
  • 254
  • 3
  • 12

2 Answers2

2

Try this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule ^ http://www.example.com%{REQUEST_URI}?domain=%1 [QSA]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

I would use:

RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} ^(www\.|)(.+)\.example\.com$
RewriteRule ^.*$ http://www.example.com/$1?domain=%2 [QSA]

This will also rewrite www.fish.example.com to ...?domain=fish.

Floern
  • 33,559
  • 24
  • 104
  • 119