Forum Moderators: phranque

Message Too Old, No Replies

.htaccess help, please

         

henry2

3:59 pm on Sep 3, 2009 (gmt 0)

10+ Year Member



Folks:

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

jdMorgan

6:24 pm on Sep 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Key truth 1) RewriteConds apply only to one RewriteRule -- the one that immediately follows the RewriteCond(s).
Key truth 2) Rules must be ordered from most-specific patterns/conditions to least-specific to avoid surprises
Key truth 3) All rules must end with an [L] flag unless, in your expert opinion, you must not use one.
Key truth 4) The regex pattern ".*" should almost never be used. It's easy to learn, but most-often not the best pattern - for many reasons.
Key truth 5) Mod_rewrite rules in .htaccess will loop 'forever', unless such looping is explicitly prevented.

RewriteCond %{HTTP_HOST} ^test\.example\.org$
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(([^/]+/)*[^.]+)\.(html¦php)$ index.php?f=$1 [L]

Replace the broken pipe "¦" character with a solid pipe character before use.

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

henry2

10:18 pm on Sep 3, 2009 (gmt 0)

10+ Year Member



jdMorgan:

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

g1smd

12:00 am on Sep 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes. Remove the NC from the rewrites.

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.

henry2

11:43 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



g1smd:

Thanks for your response.

Are you saying

TEST.EXAMPLE.ORG

should be redirected to

test.example.org

in .htaccess? It makes sense, I'm not disagreeing, but I don't know and I'm wondering: is that a webmaster's choice, a general-accepted practice, or a standards-mandated transformation? Or?

jdMorgan

12:14 am on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Best-practice: Any domain except *exactly* the correct one should be 301-redirected to the correct one in order to avoid duplicate-content issues.

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]

The allowance for a blank hostname is only really needed if your site is accessible by a unique IP address.

Jim

henry2

8:13 pm on Sep 6, 2009 (gmt 0)

10+ Year Member



JdMorgan:

Thanks! I've followed your best-practice suggestion!

I think I'm OK now. Not perfect, but OK. <grin>

Henry