Forum Moderators: phranque
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.
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]
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
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).
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
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.