JCraine
JCraine

Reputation: 1395

Mod Rewrite Subdomain to PHP with GET variable

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

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

You must capture the first part of the domain in a RewriteCond and then use this in a RewriteRule. The additional RewriteConds 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

1321941
1321941

Reputation: 2170

Should do the trick.

RewriteCond %{HTTP_HOST} ^(users)\.example\.com$
RewriteRule ^(.*)$    http://www.example.com/?username=%1 [L]

Upvotes: 0

Related Questions