Alright, so unlike other solutions I only want one subdomain to appear to be added.

Say I wanted example.com/folder/page.php to be displayed as foo.example.com/folder/page.php, how would I go about doing this?

The foo will never change, and it only needs to be for one page.

Various other sites have been unable to provide an answer, thanks in advance.

Edit: Oh, and I also have full DNS records to the domain.


You can not add a subdomain via the .htaccess file.

You need to modify the vHost configuration and/or add one more vHost.

Have a look at the documentation of your webserver to see how to do this.

EDIT: There is a logical reason behind this (I don't know all webservers and their features, so correct me if I say something wrong):

Let's look at a typical HTTP request (sent by the client):

GET /index.php HTTP/1.1
Host: www.example.org

So, we have the following information in this request:

  • the uri: index.php
  • the protocol version we understand: HTTP/1.1
  • the Host (vhost) we want the uri from

On such a request, the webserver does a few lookups:

  1. Host: first the server needs to know in which directory he should look, this is part of the vHost configuration
  2. the uri you are looking for

As the .htaccess file belongs to a vHost (eg. a.example.org), we are dependent on the vHost having already been decided.

  • Is there not some sort of solution of setting up that subdomain, and using the .htaccess on it to display the content from the base url (no subdomain)?
    – Jake
    Jun 28 '11 at 20:13
  • @Jake i will edit my comment to provide more information WHY it is not possible
    – Thomas Berger
    Jun 28 '11 at 20:15
  • But surely if it's possible to add wildcard subdomains, by editing DNS records, it is much easier to just add one? For example stackoverflow.com/questions/586129/… ?
    – Jake
    Jun 28 '11 at 20:30
  • @Jake if you look at the top answer: This is done via server alias. Here you need to modify the virtual host config too. And you need to script the "subdomains" in php, there is still no way to do this via .htaccess
    – Thomas Berger
    Jun 28 '11 at 20:37
  • 2
    @Jake Where do you host your site? Check their Control Panel -- any decent CP will allow you to add subdomains to your domain hosting and point it to any folder on your site. In other words -- use your hoster's Control Panel to configure subdomain .. and the rest (URL rewrite) can be done in .htaccess (if mod_rewrite is available).
    – LazyOne
    Jun 28 '11 at 21:28

Your Answer

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