Subdomain with mod_rewrite

software development

#1

hello,
I am trying to create a mod_rewrite for subdomains, but I am unable to be successful. I know that I can create real subdomains from the webpanel, but I dont want to do this. What I need it to create mod_rewrite subdomains to I dont have to create the same php files for each subdomain and upload it to their subdomain folders. So far I have tried this in htaccess:

Options -Indexes
Options +FollowSymlinks

RewriteEngine On
RewriteCond %{HTTP_HOST} abc.example.com
RewriteRule ^test/(.*) http://www.example.com/ahmettest/test1.php?
input=$1

which I believe should be correct. But it doesnt work. Does anybody have any idea?


#2

Well first of all, the DNS record for the subdomain needs to exist; you’ve changed your domain name, so we have no way of testing that out… perhaps you should give your real domain name?

Did you create the subdomains in the panel, but make them the mirror of the main domain? I guess that’s probably how you’d need to do it…


#3

the real domain name is hobiportali.com but my question is not about how to create subdomains from the web panel. Let me give you an example. Lets say I created a site about fruits. and I will have a content about: apple, pear and peach.

I want to have apple.hobiportali.com, pear.hobiportali.com, peach.hobiportali.com.

but instead of creating seperate php files for each of the domains and uploading them to their own directories. I want them to be redirected(with mod_rewirite) to www.hobiportali.com/fruits/index.php?fruitName=apple etc.

so this way, all of the pages will be controled from one center and when I add a feature all of the subdomain will use it automaticly.
I read it that this is possible with mod_rewrite but I coudlnt get the sample codes to work on dreamhost.


#4

Hope this doesn’t come out sounding grumpy - I haven’t had much to eat today, and as everyone who works here knows, that equals grumpy Will.

I think you missed the point of my question there, though. My question is relevant, because if you haven’t added the subdomains specifically, they probably don’t exist / resolve at all. So that’s what I was trying to ascertain.

Ok - but none of these subdomains exist. I can’t give you a real answer if you don’t give me a real example. I’m not trying to be a jerk - and I understand that this may not be stuff that you want people to look at until it’s setup and working… currently, it doesn’t look like you have setup any subdomains except one, which isn’t setup as a mirror, and points to its own subdirectory (and also appears to have been added recently).

But anyway - this is my point - apple.hobiportali.com won’t resolve unless it’s added to our system somehow. So even if you’re doing some tricky rewrite rules, the subdomain still has to resolve. Does this make sense?

You could sort of “create” subdomains that don’t actually (specifically) exist in the DNS with a wildcard DNS record (*.hobiportali.com resolves to a particular IP), but we don’t generally do this for shared hosting clients.

You have a few options.

  1. You can create all the subdomains from the panel, but set them up as mirrors of the domain itself, and then use mod_rewrite to do some magic. I don’t know that this will work, but I think it will.

  2. You add a unique IP to the domain itself, and then do NOT add the subdomains at all to the panel, but rather add custom DNS records (or have us add them for you, if you don’t have access to the custom DNS panel) pointing apple.hobiportali.com etc. to this IP, and then do your magic in mod_rewrite.

  3. You do the same thing as #2, only write to me via support and say “pretty please, will you add a wildcard DNS record for hobiportali.com for me; I understand that if I screw stuff up because I don’t know exactly how wildcard DNS records work I wold hold you responsible” - and it’s possible / likely that I may indulge you.

As far as how to do the mod_rewrite stuff, can’t really help you there - just trying to rule out some possible (unrelated) reasons why this might not be working.


#5

First of all, I am sorry, I didnt want to disrespect. Second, I chose the first option and everything seems great. Thanks


#6

Will, it may be useful to add to knowoledge base why you don’t usually offer wildcard dns to users. I was surprised of this choice until I’ve read this article (http://www.iab.org/documents/docs/2003-09-20-dns-wildcards.html) about the problems that wildcard dns entries can cause.


#7

There are some other reasons too…

  1. Allowing this could let users circumvent the limit on numbers of subdomains allowed.
  2. It won’t work with name-based virtual hosting; the wildcard entry would have to point to a domain / subdomain with a unique IP to work properly.
  3. As mentioned in the article, people sometimes make incorrect assumptions about how wildcards work, and this can cause some problems unless people know how to use them correctly.

I’ll try to add this to my “to be documented” list.

Note that while some of the arguments may apply here, the IAB article is referring to the use of wildcards in high level zones (i.e., zones like com. / net. etc.) - in this case, in response to Verisign adding their infamous “Sitefinder” wildcard record.