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

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

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

Is it possible ?

  • 2
    Do you have the sub-domains assigned in your DNS and web server?
    – closetnoc
    Aug 7, 2014 at 4:59
  • I need to do it via .htaccess dynamically.
    – Rohan
    Aug 7, 2014 at 5:07
  • 3
    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, 2014 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.
    – MrWhite
    Aug 7, 2014 at 8:13

3 Answers 3


Wildcard VirtualHost

You can use a wildcard VirtualHost statement.

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

If you have a specific host, e.g. www.example.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://*.example.com/.


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

  • 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, 2014 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. Aug 8, 2014 at 14:10

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.example
RewriteRule ^ http://site.example/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.example-host is seperate and thus doesn't run into a loop. OFC you'll end up with people accessing user1.site.example and ending up with an address bar filled with site.example/?was=user1.site.tld&uri=/... If you need something different it's probably possible too though.

  • 1
    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. Aug 7, 2014 at 18:18
  • The sub domain is dynamic which are the usernames of the registered users.
    – Rohan
    Aug 8, 2014 at 3:56

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


Here is the solution i got

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^/?$ /member.php?username=%1 [L]

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^/?contact$ /contact.php?username=%1 [L]

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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