Forum Moderators: phranque

Message Too Old, No Replies

Specific mod rewrite question

         

martindell

10:51 am on Nov 23, 2004 (gmt 0)

10+ Year Member



Hi, I'm currently rewriting one URL like so:

RewriteRule ^static_url-(91).asp /article.php?spt_id=$1 [L]

and it works fine and dandy but I'm looking for a sulution where I don't have to add a new rule for every URL that I want rewritten because the bit at the end of the URL (in this case 91.asp) is the only significant part.

Ideally what I need some help with is a rewrite rule that does this:

RewriteRule ^any_old_stuff_here-(number).asp
/article.php?spt_id=number [L]

I'm sure it's easy but I can't figure it out - any help would be much appreciated.

mipapage

10:59 am on Nov 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm no pro at this, but would this work?

RewriteRule ^static_url-(.*).asp /article.php?spt_id=$1 [L]

Basically you are turning whatever is between the brackets into the $1...

Do you have access to your server logs? The error logs are great for troubleshooting rewrite problems...

martindell

2:56 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



Thanks for the reply, that helped a lot. I eventually ended up with:

RewriteRule ^.*-(.*).asp /article.php?spt_id=$1 [L]

so that I only look at the bit in the brackets for the rewrite.

Solved!

mipapage

3:59 pm on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ha, cool, glad it helped...

jdMorgan

7:09 pm on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to throw in a suggestion after the fact...

Don't use ".*" if you can avoid it. The reason is that ".*" is the least-specific pattern possible, and it causes extra work. Looking at this code, here's what happens:


RewriteRule ^.*-(.*).asp /article.php?spt_id=$1 [L]

Mod_rewrite will take the incoming URL, and match it against the pattern.
Since the pattern starts with "^.*", the entre URL will match into the first part of the pattern.
But then, the rest of the pattern fails to match because the ".*" has consumed the entire URL.
So, mod_rewrite backs off one character.
It can now satisfy the trailing "p" in the pattern
So it backs off another character, and can now satisy the "sp" in the pattern.
Backing off another character satisfies "asp".
And another satifies ".asp"
Now, we run into a second ambiguity, since the second ".*" will match any number of any characters.
So mod_rewrite will put that aside for now, and continue backing off until it finds the "-" in the pattern.
mod_rewrite will back off another character.
If the character preceding ".asp" in the URL is a hypen, mod_rewrite will now be able to match, and the rule can proceed. However, if this character is not a hyphen, mod_rewrite will have to back off another character, and try again.
It will have to work its way backwards through the entire requested URL until it has something to match before the hyphen and something to match after the hyphen, or until it knows that there is nothing before or after the hyphen except for the trailing ".asp".

Here's an alternate way to do the same thing that does not involve so much work:

RewriteRule ^[^-]+-([^.]+).asp$ /article.php?spt_id=$1 [L]

This requires one or more characters not equal to a hyphen, followed by a hyphen, then one or more characters not equal to a period, followed by ".asp". It can be processed entirely in a forward direction with no ambiguity and no repeated "cut and try".

".*" is easy to write, and somewhat familiar, but horribly inefficient. On a busy site with a lot of RewriteRules, it can make or break your server's performance. The same is true in scripting languages that support regular expressions.

Jim

martindell

7:24 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



WOW! thanks - I would never have figured that out (actually I don't understand why or how it works but I can see that you do)

Thanks for the suggestion

jdMorgan

9:50 pm on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are some useful references cited in our forum charter [webmasterworld.com] if I haven't mentioned that already.

If anything's not clear, post! This forum is supposed to be a place to discuss techniques, although it does seem more like a "mod_rewrite repair shop" most of the time.

Jim