Reputation: 1395
I'm trying to wrap my head around mod rewrite, but can't seem to figure this out.
Is there a way I could do the following?
User types in: http://wildcard.mydomain.com
Silently writes to:
http://mydomain.com/index.php?username=wildcard
Upvotes: 2
Views: 3529
Reputation: 74028
You must capture the first part of the domain in a RewriteCond
and then use this in a RewriteRule
. The additional RewriteCond
s are there to prevent www.mydomain.com
and index.php
being rewritten
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$
RewriteRule .* /index.php?username=%1 [L]
But this is only a small part of the needed functionality. Additionally, you must setup DNS entries for all of your username.mydomain.com
domain names or setup a wildcard DNS entry *.mydomain.com
pointing to your host.
Otherwise, the client tries to contact jcraine.mydomain.com
, for example, and doesn't find a DNS entry and complains.
If this is a virtual host, you must also add a ServerAlias
for each of your usernames
ServerAlias jcraine.mydomain.com
or a wildcard catching all subdomains
ServerAlias *.mydomain.com
Upvotes: 5
Reputation: 2170
Should do the trick.
RewriteCond %{HTTP_HOST} ^(users)\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/?username=%1 [L]
Upvotes: 0