How can i set .htaccess to rewrite url like subdomain url?

hi 2 all,

i have set up wildcard dns on my domain
let’s say

now i want sub domains to work
i meant if i write

then it will redirect to that ‘test’ subdomain and seek out the index page and url will remain the same as above

if test domain not exits then will redirect to with that ‘test’ argument

actually i have tried a lot to write .htaccess file
but everytime i failed
i do not know what exactly to write in .htaccess file

any one know about this, pls pls pls let me know

i have dreamhost hosting

Web Designer and Developer

When you make this initial assertion, do you mean that you have had Dreamhost Technical Support make the necessary manual changes to their systems to facilitate wildcard DNS operation on your domain? Understand that, as indicated in the Dreamhost Wiki article on Wildcard DNS, “There is currently no way to set up wildcard DNS entries through the control panel despite having the ability to add an asterisk () record to a hosted domain. [b]An additional Apache ServerAlias must be to be added to the httpd.conf file for the Apache service that your domain is assigned to, and simply creating an asterisk () DNS record will not do this.[/b]”

  • emphasis added by me :wink:

This circumstance will work as expected whether or not wildcard DNS is set up for the domain in question, as long as “” has a DNS record (which it will if the sub-domain was set-up on Dreamhost via the Control Panel).

Getting this to happen ( being routed to if a lookup for fails) is what wildcard DNS is all about, and if it is not working without any further mucking about with re-write rules, re-directs, etc., then widcard DNS is not set-up for “”.

.htaccess re-write rules can be very powerful, but they can only work to handle requests arriving at a domain for which they have been installed - without wildcard DNS functioning “” and “” are two different domains for the purposes of .htaccess re-write rules. A request for “” will not need any re-write rules to “fail through” to “domain.comif wildcard DNS is working, but if wildcard dns does not cause this to happen, no amount of diddling .htaccess rewrite rules on “” will have any effect on the request - the webserver on will never receive or process the the request because DNS lookup for the non-existent “” will simply fail, and .htaccess rules on “” will never come into play.

Does that make any sense?


Hello Mr.Parker,

Thanks for your response.

i have contacted dreamhost team to set up wildcard dns on my domain.

they have created wildcard dns entry for that domain

when i ping any subdomain of this domain it gives me same IP as has. suppose i ping ‘’ and ‘’ both will response back with same IP.

so i can say that wildcard dns is set up.

i agree what you told
but if wildcard dns is set up and working then all sub domain will route to same index page which will on

if i try with and then both will show same index page

i want to show ‘enil’ sub domain index page which resides in domain hosting path/enil/whatever.file and it will offcourse be different from ‘parker’ sub domain index file

so for this, some stuff require on .htaccess file and i want to know that stuff


You are welcome, Enil,

I’m glad you confirmed that DH has set up the wildcard DNS for your domain. That is where it all has to start.

[quote]when i ping any subdomain of this domain it gives me same IP as has. suppose i ping ‘’ and ‘’ both will response back with same IP…so i can say that wildcard dns is set up.
I understand - (edited to remove incorrect info - I misunderstood the original poster and originally wrote an incorrect response - Oops!) This does tend to indicate the wildcard DNS has been set-up

Yes, if, and only if, neither nor exist. If either of those subdomains exist, then the appropriate index (directory listing, index page, etc) of those sub-domains will be displayed in response to the request (a request for will show the index for, and a request for will show the index for

I understand, but what I am trying to explain is that .htaccess re-write rules and re-directs is not they way to do this if path /home/enil/ and /home/enil/ are sub-domains, and /home/enil/ is your “main” domain. Re-write rules *installed in each of those “domains” will work to intercept and redirect requests received at each of those domains, but a request for one of those domains will not be received by the others - which is why you need wildcard DNS to "redirect upon fail’ if a subdomain is looked for but not found.

If exists, it is possible that subdomain DNS has not yet propagated, which would account for why you are ending up at the same index no when you browse to or

If that is not working, it does not appear that the wildcard DNS is working properly. If Dreamhost has set it up for you, it is not working correctly, and I do not believe you can “fix” it with .htaccess re-write rules.

If you have set up a sub-domain, and DNS for it has propagated, the appropriate index should be displayed if the sub-domain exists, otherwise, the index of should be displayed.

If all the set-ups have propagated and it is not behaving that way, you should contact DH again and have them investigate further.



I’m sorry, I misunderstood some of what you wrote in this post, and as a result part of my initial response was not correct - I’ve edited the post now that I understand better what you were saying.

All of this could just be the result of your sub-domains not having propagated yet?


Thanks a lot Mr. Parker

i will do check about sub domain propagation


Hello Mr. Parker,

i misunderstood wildcard dns meaning. so i must sorry to you.

ok now i got the whole thing

but the problem is still there.
my problem is :

suppose i make ‘parker’ folder in
then ftp path will be

i want to use this ‘parker’ folder as sub domain

suppose you put index file under ‘parker’ folder
so access path for that index file will be

but i want to access that file as
this above url will be shown in browser while in background it is accessing the

so i want to rewrite URL to
so anyone can treat it as a subdomain



No problem…

The easiest way to do what you want is just to change the “web directory” that is used to serve the sub-domain.

You can do this via the Manage Domains screen of the Dreamhost Control Panel. When you browse to the Manage Domains screen, and add a sub-domain (in your example “”), you can specify what directory in your user space you want to be used to serve the domain/sub-domain from. By default, DH uses “” in your user space as this directory, but you can set it to whatever you want.

For example, DH would by default set the directory for the sub-domain to “/home/username/” , similar to the way it set the web directory for “” to “/home/username/”.

If you wish, you can change the web directory for to be /home/username/ . You can define the web directory at domain/sub-domain creation time, or you can change it later by clicking the “edit” link to the right of the domain or sub-domain name in the Manage Domains screen.

This will accomplish what you have described, and as you can see, you will not need to use re-write rules to accomplish this. While this will work, I do not think it is the best way to do this, because “”, and other domains set up this way, will the inherit the .htaccess settings and permissions of the “” directory in which they are contained.

Since Dreamhost allows you to “fully host” an unlimited number of sub-domains at no additional cost, I think it is much better to simply create a “” fully hosted domain/sub-domain and set it 's web directory to the default “/home/username/” so that, as you later develop both and, you avoid the potential of conflicting .htaccess interactions (each domain/sub-domain will have its own directory “tree” beneath your user). Either way you set it up, you will use the same urls to reference the files; the differences will be in that you have a “cleaner” directory structure that could become very important as the sites evolve and various applications need to have their own .htaccess re-write rules, permissions,and other settings.

If you set all these sub-domains up as the same user, there is no disadvantage to doing this (and if you have wildcard DNS setup, it will still work). I suspect that many think of hosting a “subdomain” as a “subdirectory” and then re-writing via .htaccess as that is the only method they had available without extra hosting costs on a previous host. On Dreamhost, there really is not a reason to do it that way, and Apache is more efficient without re-writing urls.

Keeping a “site” within it’s own directory tree is just a better way to go, and gives you a lot more flexibility in managing users and security than complicated groups and permissions schemes and url re-writing if you have the ability to do so (and on Dreamhost you do have that ability).


Hello Mr.Parker

Thanks a lot for great help

you are such a nice person
i do not know you male or female


You’re Welcome, Enil. I hope my information was of some use to you, and good luck in getting things set up the way you want. :slight_smile:

(I’m male :open_mouth: )

i m too male
LOL :slight_smile:


How can one be too male? I think this conversation’s for an entirely different message board.