Forum Moderators: phranque

Message Too Old, No Replies

subdomain rewrite to main domain

         

machonemedia

7:37 am on Jan 19, 2008 (gmt 0)

10+ Year Member



Hi,

Yes another subdomain rewrite question. I wish I found the solution but did not.

I've tried about 10 different code samples (modified them as well), nothing worked.

-----------
RewriteCond %{HTTP_HOST} ([^\.]+)\.domain\.com [NC]
RewriteRule ^(.*) [domain.com...] [P]
-----------

I can go to:
"http://www.domain.com/vars" which actually shows "http://www.domain.com/vars.php?company=www

(vars.php is just a test page to see if I can transfer the subdomain into a variable in my script)

This code won't work with any text other than "www"...

I also know that my 2nd line should be an internal syntax but it won't work with any I've tried.

Only the full domain path and [P] flag seems to let it work for "www".

Also, you should note I have many PHP pages that are rewritten to /login/ goes to /login.php, etc.. so the subdomain variable reference will have to allow for these pages to be dynamically shown I guess. Like I have it already where it takes the string .com/"var" and uses it as the PHP filename, so .com/login will go to .com/login.php.

Some PHP scripts have multiple variables be rewritten into them also.

The most complex is:

RewriteRule ^profile/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/$ profile.php?id=$1&nw=$2&yearID=$3&monthID=$4 [NC]

This is the closest I've been able to get, any help is greatly appreciated.

Thanks.

jdMorgan

6:30 pm on Jan 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The phrase "doesn't work" is a thread-killer here. Unfortunately, when analyzed, it is devoid of any useful information.

Your code shows a redirect to "example.com" but you then refer to "www.example.com" later. If you also have a redirect from non-www to www.example.com in your code, then your rule will loop forever, with your posted rule removing "www" from the hostname and putting it into the query string, then the domain redirect will execute, adding www back on to the hostname, and then your posted rule will execute again, removing it from the hostname and putting it back into the query string. This will repeat until the client or the server reaches its redirection limit.

An additional problem is that each time this happens, the code adds another ".php" to the script filepath.

We also need to know whether you have configured DNS to support your added subdomains, and whether your server has been configured to 'map' those subdomains into your 'main' domain's filespace, or into separate filespace(s). Various hosts do this differently.

Assuming that all subdomains are mapped to the root of your main domain's filespace, a good general (and simple) solution would be something like this:


RewriteCond $1 !-co\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com
RewriteRule (.*) /$1-co.php?company=%2 [L]

The "-co" appended to the company name does two things; First, it is used by the first RewriteCond to prevent this rule from looping, and second, it allows you freely name your own internally-used and/or shared scripts without fear of colliding with a company-named script.

This rule can be simplified --and other problems avoided-- if you precede it with a domain canonicalization redirect rule. That is, redirect non-www requests to www (or vice-versa) before doing this internal subdomain rewrite stuff.

Jim

machonemedia

11:44 pm on Jan 19, 2008 (gmt 0)

10+ Year Member



Must be server configuration.

I have 1 server (shared hosting). The account allows me to have multiple domains pointing to different directories.

So example.com would actually grab files from "/root/example/".

So this is kind of working already but at a root level, I have a new domain that us in "/root/newdomain/" but I can access files in there from [newdomain.com....] The problem is making subdomains from that domain so I can have [test.newdomain.com...] actually display the file "root/newdomain/login.php".

I will ask them what the settings are at, or I will try to find them in cPanel.

When I use your code and enter [test.domain.com...] I get the cPanel success page saying my server was setup successfully.

When entering [test.domain.com...] I get a 404 error.

I don't have any "company-named" scripts. I think it's my server configuration. I will send in a support ticket and find out how to best get this to work (if even possible on my server).

jdMorgan

12:41 am on Jan 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When configured as you describe, files in the "main" domain's root directory are not accessible via HTTP. You can use *nix symlinks to create 'virtual copies' of those files in each subdirectory as needed. Also, for PHP scripts only, the PHP configuration file allows you to specify the root PHP directory, so that may help.

I used the phrase "company-name scripts" to refer to the PHP scripts you are trying to rewrite to, as in "<subdomain>.php?company=%1"

Jim

machonemedia

8:25 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thanks for your help.

I tried putting a asterisk "*" in my cpanel subdomain configuration and it is allowing me to route all subdomains to my main root directory.

I reverted back to my original code and it works, but the [P] flag (from what I saw) is an external request flag?

Is there anyway to optimize this code for interal use only?

RewriteCond %{HTTP_HOST} ([^\.]+)\.domain\.com [NC]
RewriteRule ^(.*) [domain.com...] [P]

Thanks.

jdMorgan

8:50 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, I believe I posted the solution above...

Jim