Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to use a particular page in my site as any subdomain. I need something like this http://*.site.com/.

So, as the user types http://user1.site.com/ or http://user2.site.com/ then the page must shown will be present in http://site.com/profile.php.

http://user1.site.com/ must point to http://site.com/profile.php NOT REDIRECT

Can it be possible ?

share|improve this question
Do you have the sub-domains assigned in your DNS and web server? – closetnoc Aug 7 '14 at 4:59
I need to do it via .htaccess dynamically. – Rohan Aug 7 '14 at 5:07
You won't be able to without having the sub-domain defined and resolving to an IP address and the web server knowing that the sub-domains are sites on your server. From there, you can do anything you want .htaccess wise. – closetnoc Aug 7 '14 at 5:23
As closetnoc says. By the sounds of it you need a "wildcard subdomain" - does your host support this? Once that is set up and resolving then you can use .htaccess trickery. – w3dk Aug 7 '14 at 8:13

Are you talking about sharding? Look at "Advanced Apache Rewriting" - at least probably your solution will use a RewriteMap .. although you do say dynamically, I still assume your list of users is static. RewriteMap Apache Documentation should be the way to go - you put your mapping in a text file like so:

Ralf.S.Engelschall    rse   # Bastard Operator From Hell
Mr.Joe.Average        joe   # Mr. Average

Then you define this map in the .htaccess/apache-conf:

RewriteMap real-to-user txt:/path/to/file/map.txt

Then you use that mapping in a rewrite rule:

RewriteRule ^/ex/(.*) ${examplemap:$1}

Another way - if you want to check inside that profile.php you mentioned - would be to do the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} !=site.tld
RewriteRule ^ http://site.tld/profile.php?host=%{HTTP_HOST}&uri=%{REQUEST_URI} [R=301,L]

You should have your setup so that your "catch all"-host has that RewriteRule, but your site.tld-host is seperate and thus doesn't run into a loop. OFC you'll end up with people accessing user1.site.tld and ending up with an address bar filled with site.tld/?was=user1.site.tld&uri=/... If you need something different it's probably possible too though.

share|improve this answer
This will work only if your server is configured in a way such that *.domain.com resolves to the host with your rewrite rules. If you have that setup, then I am not sure you need any rewriting. – jeffatrackaid Aug 7 '14 at 18:18
The sub domain is dynamic which are the usernames of the registered users. – Rohan Aug 8 '14 at 3:56

Wildcard VirtualHost

You can use a wildcard VirtualHost statement.

<VirtualHost *:80> DocumentRoot /path/to/doc/root ServerName *.domain.com </VirtualHost>

If you have a specific host, e.g. www.domain.com, put its VirtualHost stanza above the wildcard one. Apache works on a first match basis.

Also, you may want to add:

DirectoryIndex profile.php

to either a .htaccess or your VirtualHost if you want that file to be served up by default when no file is specified.

This will be needed to serve profile.php when a user type http://*.domain.com/.


You can then setup a wildcard DNS entry for *.domain.com or use CNAME/A record for each user.

share|improve this answer
Can it be possible only through .htaccess ? I have searched throughout the internet & got many codes but didnt got exactly what i am looking for. – Rohan Aug 8 '14 at 4:04
The server has to be configured to route *.domain.com to your document root. There are multiple ways to do that but it requires a server level change. If you have a dedicated IP address it may work if the server maps that IP exclusively to your document root. But you really need to find out how the server is setup. – jeffatrackaid Aug 8 '14 at 14:10

While searching through out the internet, i got this link.


Here is the solution i got

RewriteCond %{HTTP_HOST} !^www.domain.com

RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]

RewriteRule ^/?$ /member.php?username=%1 [L]

RewriteCond %{HTTP_HOST} !^www.domain.com

RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]

RewriteRule ^/?contact$ /contact.php?username=%1 [L]

RewriteCond %{HTTP_HOST} !^www.domain.com

RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]

RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=$1&album_id=$2 [L]

But it is exactly not same as my requirement. I need to change this code as my need.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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