Forum Moderators: phranque
These .htaccess incantations
rewritecond %{http_host} ^test\.example\.org$ [nc]
RewriteRule ^(.*)\.html index.php?f=$1 [nc]
accomplish most of what I want: urls of the form
test.example.org/fie/fum.html
are processed to this form:
test.example.org/index.php?f=fie/fum
But I have been completely unsuccessful modifying the incantation so that urls like this:
test.example.org/fie/fum.php
are are also processed to the same form:
test.example.org/index.php?f=fie/fum
I've tried simply adding a RewriteRule like this
RewriteRule ^(.*)\.php index.php?f=$1 [nc]
in many variations, but none of them succeed, not even close.
What am I doing wrong? What key truth about .htaccess am I totally missing?
TIA,
Henry
RewriteCond %{HTTP_HOST} ^test\.example\.org$
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(([^/]+/)*[^.]+)\.(html¦php)$ index.php?f=$1 [L]
Do not accept a non-canoncial hostname in this rule. If there is a capitalization error in HTTP_HOST, then redirect it to the canonical domain. Similarly, do not accept capitalization errors in .html or .php -- redirect them to force the correct case.
Jim
Thanks for the fast and complete reply.
I have looked through lots of web pages about .htaccess but I haven't seen anything like the first four of your Key Truths. Excellent and practical advice!
Thanks for the code to solve my problem. I studied it until I understood it fully, then I added a modification to accommodate urls of the form
test.example.org/fie/fum.i.html
which is part of my design that I didn't mention. Here's the resulting RewriteRule:
RewriteRule ^(([^/]+/)*[^.]+(\.i)?)\.(html¦php)$ index.php?f=$1 [L]
Also thanks for the extra fix to avoid recursion -- I already had something downstream to take care of that. Now that I see how easy it is, I think .htaccess is the place to do it.
I think I understand the point about non-canonical hostnames: remove nc everywhere and let people deal with it: case is part of spelling.
Problem solved, .htaccess newbie educated!
Thanks,
Henry
Set up a redirect ahead of this rewrite to redirect wrong case, etc, to the correct FQDN. The redirect will be a separate rule.
So people can use either URL to get access, but requests for the 'wrong' URL will be redirected to make a new request for the 'right' URL before content is served.
Examples (try them) :
test.example.org.
test.example.org:80
test.example.org.:80
Basically,
RewriteCond %{HTTP_HOST} !^(test\.example\.org)?$
RewriteRule ^(.*)$ http://test.example.org/$1 [R=301,L]
Jim