Forum Moderators: phranque

Message Too Old, No Replies

Why do mod rewrite rules act so differently?

Works on this server, not that

         

rocknbil

10:40 pm on Oct 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take two servers from the same hosting service. Theoretically, the same setup, CentOS with WHM/Cpanel for management.

A (probably shoddy) mod_rewrite rule

RewriteRule ^Catalog/*[-_a-z0-9]*$ /some-dir/some-script.php [L]

Which captures (the noslash is canonicalized above, case sensitive is intentional)

/Catalog
/Catalog/something
/Catalog/something-else
/Catalog/something-else/some-item

But on the second server, fails. It's related to the possible dash in the second URL part, "somethingelse" works fine, "something-else" fails.

I played around with it a bit and will eventually solve it, but the question is not so much "fix my rewrite" as it is "why are things so different on various servers?" Is it the PCRE engine, and how would I determine where to look for differences that slow me down?

lucy24

11:04 pm on Oct 25, 2011 (gmt 0)

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



If you've given the rule exactly as written, it doesn't capture anything. Where are the parentheses?

There are other problems.

rocknbil

5:06 pm on Oct 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rule works perfectly on one server and not another. I don't need to "capture," I guess the better word is "rewrites." The script manages all. The last example obviously won't work, no / in the class, but it doesn't matter - the question is not about the rewrite, but differences from one seemingly similar server to another.

lucy24

9:41 pm on Oct 26, 2011 (gmt 0)

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



Just to satisfy a hunch: does it behave differently if you change your group to read [\-_a-z0-9] with escaped hyphen? It's unneccessary bordering on wrong, but I'm curious.

g1smd

11:22 pm on Oct 26, 2011 (gmt 0)

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



Move the hyphen to be last item in the character group: [a-z0-9_-]

^Catalog/*[-_a-z0-9]*$ -- the /* allows a match for example.com/Catalog//////////////012345

The rules work exactly as they are coded not how you hope they might work.

Since multiple URLs are rewritten to be serviced by one script, you need to make sure that the script can return a proper 404 header for non-valid requests.

rocknbil

4:02 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe I shouldn't have posted an example rule . . . there are other rewrites that work fine on one server or another but on some fail.

The question is what are the most likely factors for it reacting differently on different servers?

[\-_a-z0-9]


No, no difference. as I said twice, the rule works fine on one (actually several) other servers, that's not the question.

[a-z0-9_-]


Had (Have) it that way on the "working server(s)" (there are several domains across several servers,) the above is a fiddle-about experimenting with the "doesn't work" server. Actually it's this, because the path may contain sub paths

^Catalog[/a-z0-9-_]*$

All working fine on the "working servers," and yes, script outputs 404's and a not found page for invalid entries (which I took from your advice years ago lol . . .)

g1smd

5:44 pm on Oct 27, 2011 (gmt 0)

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



Hyphen next to digit may confuse the parser. Hence the suggestion for [a-z0-9_-] here.

lucy24

7:23 pm on Oct 27, 2011 (gmt 0)

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



there are other rewrites that work fine on one server or another but on some fail.

Can you post some more examples? There may be a pattern that will jump up and hit someone in the face. And once you've pinned down the pattern it's easier to isolate the problem.

You have probably already considered the possibility that one of your servers has a resident gremlin. Mine does, and they ended up deciding that extermination was more trouble than it's worth. (The current manifestation is that a certain type of RewriteRule goes into an infinite redirect-- which leads to the 403 you originally asked for. Server time is cheaper than technician time.)