Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite pointing subdomain to root file

         

AdamR

9:10 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



I'm looking to point a subdomain to a file in the root folder, and any file after the subdomain, also redirected to the file of the same name in the root folder, for example:

[subdomain.example.com...] would be redicrected to [example.com...]

and [subdomain.example.com...] would be redirected to [example.com...]

Any advice how I could go about this? I'm assuming mod_rewrite is the proper method to use.

Any help much appreciated.

- Adam

AdamR

9:11 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



I think I mixed up my words in my above post. By redirected, I mean rewritten. I want the URL to the file be changed on the server level, not client-side (redirected). :)

- Adam

jdMorgan

9:15 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adam,

Welcome to WebmasterWorld!

Check out the mod_rewrite and regular expressions references cited in our charter [webmasterworld.com] to get started.

You will need to use
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
to find and create a back-reference to the subdomain name for use in the RewriteRule.

Jim

AdamR

1:14 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Sorry to be a complete and utter noobie to this stuff. I tested out a few things, no luck so far. If anyone could provide additional information, or the actual text I would need to use for the example given above, that would be much appreciated.

- Adam

jdMorgan

1:24 am on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your code. As stated in charter, we can't write it for you, but we'll help to fix any honest effort.

Jim

AdamR

1:42 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Here's the last one I tried:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)subdomain\.example\.com
RewriteRule ^([^./]+)/$ /index.php
RewriteRule ^([^./]+)$ /$1

It's probably completely syntactically incorrect and not even close to what it should be.

- Adam

jdMorgan

2:25 am on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops! I don't think I understood your question. Do you want to rewrite each subdomain to a file or subdirectory specific to that subdomain, or just rewrite any subdomain (warning - usually excepting the "www" subdomain) to the same set of files?

It's not clear what you're trying to accomplish with this project.

Jim

AdamR

2:39 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Here is what I'm looking for as best as I can describe it.

When going to a subdomain (http://subdomain.example.com), I would like it to rewrite the URL on the server level (not redirect) to a file in the root directory, not any sub directory under the root folder, as most subdomains work.

For example, [subdomain.example.com...] would display [example.com...]

This would be one static rewrite (this one subdomain by itself with no file specified) would direct to one specified file, never changing.

In addition to this, I would like to have a rewrite for any file specified with the subdomain to point to a file of the same name in the root folder.

For example, [subdomain.example.com...] would direct to [example.com...]

So whatever you specify as the file with the subdomain would go to the file in the root directory with that same name. So it basically, in this case, dropped the subdomain.

I know, it's a weird setup, and I hope I've explained it so you can understand.

- Adam

jdMorgan

3:42 am on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The confusing part is that in and of itself, a subdomain ceases to have any 'real' meaning as soon as it is resolved to a server by the DNS system. So, left alone, a subdomain *will* resolve to the root folder of your Web space.

So, the question here is, what mechanism is causing your subdomain to *not* resolve to your Web root folder?

If you're stuck with some "control panel" or if your host has put code into httpd.conf to 'map' your subdomain to a subfolder, then using a rewrite may not be possible, and you'll have to redirect to the main domain from the subfolder. With 'control panel' subdomains, security considerations often prevent rewriting 'up' from a subdomain's subdirectory into the main domain's directory.

Actually, a possible way out if that is the case is to rewrite some or all of the URLs for the main domain into the subdomain's subdirectory, and move some or all of the main domain's files into that subdir. Sometimes you have to grab either end of the rope that you can...

Sorry for all the back-and-forth, but the specifications have to be nailed down before any code can be generated.

By what mechanism are requests for the subdomain URLs now being mapped into a subdirectory?

Jim