6

I want to create folders on the fly, but make it seem like I am creating subdomains on the fly using mod_rewrite. e.g.

Create "john" folder using PHP

www.example.com/john/ 

Then be able to access whatever I put in there at:

john.example.com 

Thank you in advance,

Kris

2
  • Hi I'd like to do something like this potentially did you get this working? Commented Jun 27, 2009 at 1:17
  • I haven't go this working yet. I am switching over to a Plesk system soon, which will allow greater control over my server. Once I figure it out I will post the tutorial on my blog and answer this question with the link to the tutorial. Commented Jun 29, 2009 at 21:27

1 Answer 1

10

First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:

RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ RewriteRule ^ %1%{REQUEST_URI} [L] 
Sign up to request clarification or add additional context in comments.

4 Comments

Might be worth mentioning external maps to recognize invalid subfolders/subdomains :)
Thank you for the quick reply. I really don't know how to do any of this... First, the mod_rewrite code you gave me, where would that go in relation to my folder structure in the original question. Second, how would I do the configuration to my server to accept any subdomain? As you can tell I'm at the early stages of using mod_rewrite and I need a little help. Thank you Gumbo
Considering that you might want to ask your question on serverfault.com
To accept the sub-domain (it's really just a hostname), that part goes in your DNS zone. The mod_rewrite rules go in your Apache configuration for the virtual host, which also needs ServerAlias *.example.com.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.