views:

832

answers:

2

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

A: 

you need to do a rule that : *.domainname.com go to your server, and in the server do mod rewrite rule, that take the name before point and put it after the url, but it is not secure to do that.

Haim Evgi
+3  A: 

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]
Gumbo
Might be worth mentioning external maps to recognize invalid subfolders/subdomains :)
Aiden Bell
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
Torez
Considering that you might want to ask your question on serverfault.com
Gumbo
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.
Steve Madsen