Subdomain rewriting


#1

Hi all,

My questions might be simple, but I can’t find a way to do it. Let’s say [color=#000080]string[/color] is a random string.

Whenever someone goes to http://[color=#000080]string[/color].mydomain.com, I’d like it to display http://www.mydomain.com/?var=[color=#000080]string[/color] (without changing the displayed URL). Basically, it’s URL rewriting but across subdomains.

I have ZERO idea on how to do that. Any help?

Thanks!

Simo


#2

Possible solutions (Not tested)

You will get “var=string.mydomain.com” instead of “var=string”.
But it’s easy to drop “.mydomain.com” in CGI/PHP/Whatever.

Options FollowSymLinks
RewriteEngine On

# Loop stopper
RewriteCond %{QUERT_STRING} ^var
RewriteRule .* -  [L]

# Subdomain rewrite
RewriteRule .* /?var=%{HTTP_HOST}  [L]

Another example (trying to get “string” with back reference).
You may need to escape the dot in square bracket “[^.]”. I don’t remember the detail.

Options FollowSymLinks
RewriteEngine On

# Loop stopper
RewriteCond %{QUERT_STRING} ^var
RewriteRule .* -  [L]

# Subdomain rewrite
RewriteCond %{HTTP_HOST}  ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule .* /?var=%1  [L]

And you may want to take a look at URL rewriting guide and other documents.
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

mod_rewrite is very useful but pretty easy to mess up, too.
So, please be very careful.


#3

What you want is to do is more complicated than a simple rewrite. The new URL will have to pass through the proxy module, which AFAIK DreamHost does not allow on its shared servers. Also even if you had a wildcard DNS entry the web server has to be configured a certain way, which again can’t be done on shared web servers.


#4

You can just setup subdomains and use the same user to do this (unless OP wants to use lots of subdomains).
No need for mod_proxy, Wildcard DNS. It’s tedious, but pretty easy for limited number of subdomains.

In addition, although DreamHost doesn’t support WildCard DNS on shared servers, it has been done on popular shared hosts.
(I’m not really recommending or asking for this at all, though. I’m just talking about the technical possibility.)