5

Possible Duplicate:
Apache rewrite based on subdomain

Is it possible to rewrite a URL like this?

www.foo.website.com/some-page

to

www.website.com/some-page?var=foo

I'm asking about a rewrite (I think that's what it's called) and not a redirect. i.e. I want the first URL to stay in the address bar, but for the server to "get" the second URL.

I've never understood how to do URL rewrites and so far my pathetic attempts to get this working have failed miserably :-)

I should add that this .htaccess file is also being used by WordPress and has the following code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~kbj/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~kbj/index.php [L]
</IfModule>

Any help or advice would be very much appreciated!

Community
  • 1
  • 1
Nate
  • 26,164
  • 34
  • 130
  • 214

3 Answers3

2
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.website\.com$
RewriteRule ^(.*)$ "http://www.website.com/$1?var=%1" [P]

Use the P flag to proxy the request.

http://httpd.apache.org/docs/current/rewrite/flags.html#flag_p

Manish
  • 3,472
  • 1
  • 17
  • 16
  • Thanks for your reply. I tried using the code you suggested, but unfortunately it did not work. I tried removing everything from the .htaccess file except for http://pastebin.com/4i7LKn48 and then browsing to www.test.website.com, and I get a "Server not found" error. Any idea why it's not working? Thanks again for your help. – Nate Mar 11 '12 at 23:10
2

What I'll explain is for multilanguage websites, but you can very easily apply this to what you're asking: rename "LANGUAGE" to "var" (smile).

Here's what you may want to do, to be able to handle in the future many sub languages. Check your host: if it contains a "known language", add it in a global variable (= environment variable from Apache's point of view) then at the end, if the "language" variable is not set, set is as the default language. And finally, add it as a parameter.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.website\.com$
    RewriteRule ^(.*)$ "http://www.website.com/$1?var=%1" [P]

    RewriteCond %{HTTP_HOST} ^(www\.)?(us|fr|pt)\.mydomain\.com$
    # Create an environment variable to remember the language:
    RewriteRule (.*) - [QSA,E=LANGUAGE:%2]
    # Now check if the LANGUAGE is empty (= doesn't exist)
    RewriteCond %{ENV:LANGUAGE} ^$
    # If so, create the default language (=es):
    RewriteRule (.*) - [QSA,E=LANGUAGE:es]
    # Add the language to the URI (without modifying it):
    RewriteCond (.*) $1?language={ENV:LANGUAGE} [QSA]

</IfModule>

(note: please, please... use proper indentation).

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
0

Try this:

RewriteCond %{HTTP_HOST} ^([^.]+).domain.com$
RewriteRule (.*) http://www.domain.com/$1?subdomain=%1

It takes everything it finds as a subdomain and maps it to the main domain's file with the sub domain name attached as a get parameter.

gspatel
  • 1,128
  • 8
  • 12