2

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
3
  • Yes there is. What actually you cannot get?
    – zerkms
    Mar 11, 2013 at 3:32
  • Before using .htaccess, i think, you should create a general virtual subdomain with using asterisk * in your host.
    – Amir
    Mar 11, 2013 at 5:05
  • I tried your suggestion and set a wildcard for a subdomain, and set it to redirect to something that exists. For some reason, just fails.
    – JCraine
    Mar 11, 2013 at 5:22

2 Answers 2

5

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
1
  • Thanks for this Olaf. I really appreciate your thoroughness. I spoke to my host and found there was a problem with wildcard domains not going through. All resolved now so was able to get your code to work nicely. For the moment, I'll use the wildcard method. But will most likely need to setup the other stuff so users can point their own domains at their personalised app space. Your info will help me figure out how to go about it. Cheers!
    – JCraine
    Mar 12, 2013 at 12:58
0

Should do the trick.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.