Forum Moderators: phranque

Message Too Old, No Replies

rewrite a subdomain to real domain with var?

I want sub.domain.com/* to go to www.domain.com/*?var=sub

         

JustJames

10:58 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



Hello, I'm trying to rewrite a subdomain to the main domain but with a var that says what subdomain it came from. The purpose of this is to have state-based subdomains that will all call the same script/page but search only in that state.

eg: I want vic.domainname.com.au/search.php to go to www.domainname.com.au/search.php?state=vic and likewise with all the states. (I'm from Aus...)

I'm trying this:

RewriteCond %{HTTP_HOST} ^vic\.domainname\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.vic\.domainname\.com\.au$
RewriteRule ^(.*)$ /$1 [L]

Which I think should just do the redirect, but even that gives me an error.

Also, I can turn subdomains on and off through my server host, and I seem to get different errors when they're on or off - does anyone know whether they should be on or off and how I can write my rewriterule?

Thanks!

jdMorgan

4:00 am on Aug 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Also, I can turn subdomains on and off through my server host

The solution to this problem depends entirely on just what "turn subdomains on and off" means, since it's so ambiguous as to be meaningless. Hosts try to make something complicated simple. But they try to make it too simple, so you have to ask them what it means at the httpd.conf or .htaccess configuration level.

The main problem with the rule is that it rewrites the requested URL-path to itself recursively. And that's not what you said you wanted to do anyway, even if it wasn't an infinite loop.

Based on your description, I'd start with:


RewriteCond %{QUERY_STRING} !&?state=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example\.com\.au
RewriteCond %{HTTP_HOST} ^www\.([a-z])\.example\.com\.au [OR]
RewriteCond %{HTTP_HOST} ^([a-z])\.example\.com\.au
RewriteRule ^search\.php$ search.php?state=%1 [L]

Note that you are not rewriting a subdomain to the main domain. domains are meaningless inside the server; You are rewriting script requests in a subdomain to a query string variable. All that is required in addition to this code is that subdomains be pointed to your server via DNS, and that your server recognize the subdomains, and pass requests for them to your "main" domain's filespace. To use this method, you DO NOT want each subdomain pointed to its own subdirectory -- point them all to the main directory.

Jim