Forum Moderators: phranque
I've been trying to get my mod_rewrite script working and am having no luck and thought i'd give WebmasterWorld a shot...since i collected a lot of info about here anyway....
I am trying to rewrite subdomains into queries for the top-level domain...
e.g. www.purple.colours.com rewrites to www.colours.com/show-colour.rhtml?colour=purple
I have made the neccessary change in the http.conf file for Apache to allow *.colours.com (wildcard sudomains)- well, i got my admin to do it.
my rewrite looks like this...
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST}!^(www\.)?colours\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\.colours\.com$ [NC]
RewriteRule .* /show-colour.rhtml?colour=$1 [L]
now, any time i go to www.purple.colours.com, the index.rhtml page in the top-level directory (public_html for the top-level domain)always comes up instead of show-colour.rhtml.
I have a feeling i'm missing something thats simple, but i'm bangin my head here and could use some help if available.....thanks
In addition, your code won't accept "www.purple.example.com" as written. You can fix that, and simplify your first RewriteCond as well:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([\.]+)\.example\.com
RewriteRule .* /show-colour.rhtml?colour=%2 [L]
Jim
i plugged it in and it still seems to be ignoring the last line, thus presenting the index.rhtml file......
is it possible that my admin made all the wildcard domains point to the '/index.rhtml' or '/', thus the last rule is ignored - i'm thinking no, b/c isn;t that the main function of mod-rewrite - overwrite the server defined rules?
Some standard testing questions:
The ruleset as shown should internally rewrite requests for www.purple.example.com or purple.example.com to /show-colour.rhtml?colour=purple as long as the code resides in a directory-path that is accessed for those requests. If your admin used a 'control panel' to set up your wild-cards, it's possible that they are pointed to a different directory or directories than the 'main' domain and www subdomain. If that is the case, then the code won't be executed and can have no effect on the request.
Jim
Do you have other RewriteRules in this file? No
What URL did you request when testing? (please substitute 'example.com' for posting here) www.purple.example.com
What were the results when you requested that URL? no results, it just displayed the index.rhtml page rather than show-colour.rhtml
How did the results differ from your expectations? i expected it to work and it didn't - again, index.rhtml showed and not show-colour.rhtml
Is there any indication of the problem in your Server Error Log? error_log has no erros that relate to this, just old errors from before i implemented the rewriting.
Did you flush your browser cache before each test to prevent seeing stale results? yes
I'm waiting to hear back if my admin set the wildcard subdomains to point to some other directory or directories....ill keep you posted....thanks jim!...very much appreciated.
RewriteCond %{REQUEST_URI} !^/show-colour\.rhtml
Jim
The "www." is optional in the second RewriteCond, while the first RewriteCond prevents rewriting "www.example.com" to a subdirectory. Note that %1 may contain "www" if present, but the back-reference in the RewriteRule is to %2, which would contain the subdomain.
As shown, the only requests it won't accept are for subdomains like "purple.www.example.com"
Jim
You may also need to add another RewriteCond at the top, in case your code is looping:RewriteCond %{REQUEST_URI}!^/show-colour\.rhtml
Otherwise, the code will rewrite /show-colour.rhtml to itself repeatedly, until the browser or server hits its redirection limit (I would expect an error log entry in this case, though).
I now have and tested this...
[2]
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI}!^/show-colour\.rhtml
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([\.]+)\.example\.com
RewriteRule .* /show-colour.rhtml?colour=%2 [L]
[/2] Try temporarily changing the syntax so that an external redirect is invoked. This will allow you to see the code working if the rule is applied:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/show-colour\.rhtml
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([\.]+)\.example\.com
RewriteRule .* http://www.example.com/show-colour.rhtml?colour=%2 [R=302,L]
RewriteRule ^file_that_does_not_exist$ http://www.example.com/file_that_does_exist [R=302,L]
This will indicate whether mod_rewrite is even being invoked.
There is no need to escape characters in the substitution URL. This is only necessary within regular-expressions patterns.
Jim
What was the outcome of the last two examples jdMorgan had you try?
Did the external redirect actually occur? If so, what was in the query string?
What about the lone redirect rule?
Or did you still get nothing at all, but also no error logs?
[edited by: Philosopher at 5:31 pm (utc) on Aug. 15, 2006]
i ran a test using...
[2]RewriteRule ^colour/(.*)$ http://www.example.com/$1.rhtml [R=302,L][/2] i typed into the FF browser...
[2]http://www.example.com/colour/purple[/2] and got a 404 for /purple.rhtml
i cleared my cache each time i tested.
so, from this, we know mod_rewrite is working - it must be something in that last rule we are missing....
when i tested Jim's code, the redirect didn't occur - i assume i would have seen the url change from..
www.purple.example.com to www.example.com/show-colour.rhtml?search=purple
...which it did not, it stayed the same (www.purple.example.com)
for the second, single line test....it appeared to work...it generated a 404 since i was testing for a file that wasn't there...
i used...
RewriteRule ^colour/(.*)$ http://www.example.com/$1.rhtml [R=302,L]
...as my rule
i typed into the FF browser...
http://www.example.com/colour/purple
...and got a 404 for /purple.rhtml
i cleared my cache each time i tested.
so, from this, we know mod_rewrite is working
jdMorgan is definitely far better at this stuff than me but it sure seems like the information being returned in the HTTP_HOST variable is not what is being expected and tested for.
I'd check into exactly what is being returned in the HTTP_HOST variable.
Under the "apache settings" section, check what is shown for HTTP_HOST
<?php
phpinfo();
?>
Just be sure to later remove it as it shows all the environmental variables for your server.
If this isn't a live site. You could replace your index file with this code and see what the HTTP_HOST variable is returning for full domain request, subdomain requests etc. as well since everything seems to pull up your index file.
I'm wondering...can you access the file with the code I gave you by something like
www.purple.example.com/phpinfo.php
?
If so, you will want to see what it shows as the HTTP_HOST variable when accessing it via a subdomain.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/show-colour\.rhtml
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?[b]([^\.]+)[/b]\.example\.com
RewriteRule .* http://www.example.com/show-colour.rhtml?colour=%2 [R=302,L]
Jim
[edited by: jdMorgan at 9:22 pm (utc) on Aug. 15, 2006]
So the proper HTTP_HOST variable is being returned yet the rewrite is still not happening even though we know that mod_rewrite is active on your server.
I'm stumped. The code should be working then unless something your host has setup in the config files is interfering with it.
Maybe jdMorgan or someone else will have another idea. If not, I'd check with the support department of your host and give them the code and see if they know of something in their setup that would be interfering with it.