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


#1

hi 2 all,

i have set up wildcard dns on my domain
let’s say http://www.domain.com

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 http://www.domain.com 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

Thanks
-ENIL
Web Designer and Developer


#2

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 “test.domain.com” has a DNS record (which it will if the sub-domain was set-up on Dreamhost via the Control Panel).

Getting this to happen (test.domain.com being routed to domain.com if a lookup for test.domain.com 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 “domain.com”.

.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 “test.domain.com” and “domain.com” are two different domains for the purposes of .htaccess re-write rules. A request for “test.domain.com” 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 “domain.com” will have any effect on the request - the webserver on domain.com will never receive or process the the request because DNS lookup for the non-existent “test.domain.com” will simply fail, and .htaccess rules on “domain.com” will never come into play.

Does that make any sense?

–rlparker


#3

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 domain.com has. suppose i ping ‘parker.domain.com’ and ‘domain.com’ 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 domain.com

if i try with enil.domain.com and parker.domain.com 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

-ENIL


#4

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 domain.com has. suppose i ping ‘parker.domain.com’ and ‘domain.com’ both will response back with same IP…so i can say that wildcard dns is set up.
[/quote]
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 enil.domain.com nor parker.domain.com 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 enil.domain.com will show the index for enil.domain.com, and a request for parker.domain.com will show the index for parker.domain.com).

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/enil.domain.com/ and /home/enil/parker.domain.com/ are sub-domains, and /home/enil/domain.com 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 test.domain.com exists, it is possible that test.domain.com subdomain DNS has not yet propagated, which would account for why you are ending up at the same domain.com index no when you browse to test.domain.com or domain.com.

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 test.domain.com sub-domain, and DNS for it has propagated, the appropriate index should be displayed if the sub-domain exists, otherwise, the index of domain.com 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.

–rlparker


#5

Enil-

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?

–rlparker


#6

Thanks a lot Mr. Parker

i will do check about sub domain propagation

-ENIL


#7

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 domain.com
then ftp path will be domain.com/parker/

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 domian.com/parker/index.file

but i want to access that file as http://parker.domain.com/index.file
this above url will be shown in browser while in background it is accessing the domian.com/parker/index.file

so i want to rewrite domian.com/parker/index.file URL to http://parker.domain.com/index.file
so anyone can treat it as a subdomain

-ENIL


#8

Enil,

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 “parker.domain.com”), 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 “sub-domain.domain.com” 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 parker.domain.com to “/home/username/parker.domain.com” , similar to the way it set the web directory for “domain.com” to “/home/username/domain.com/”.

If you wish, you can change the web directory for parker.domain.com to be /home/username/domain.com/parker/ . 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 “parker.domain.com”, and other domains set up this way, will the inherit the .htaccess settings and permissions of the “domain.com” 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 “parker.domain.com” fully hosted domain/sub-domain and set it 's web directory to the default “/home/username/parker.domain.com” so that, as you later develop both domain.com and parker.domain.com, 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).

–rlparker


#9

Hello Mr.Parker

Thanks a lot for great help

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

Thanks
-ENIL


#10

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:

–rlparker
(I’m male :open_mouth: )


#11

Thanks
i m too male
LOL :slight_smile:

-ENIL


#12

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

Wholly