Welcome to WebmasterWorld Guest from 54.144.77.26

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod_rewrite problems

greedy, greedy

     

bonanza

1:01 am on Apr 28, 2003 (gmt 0)



I'm trying to map the following URI syntax:

/widget-A-B-C-D.html

to:

script.php?a=widget&b=A-B-C-D

when I write my rule like this:

RewriteRule ^(.*)-(.*).html script.php?a=$1&b=$2

widget-A-B-C goes into $1

My filename can have 1, 2, 3 or 4 parameters (A-D). Do I need to write one for each case, like this:


RewriteRule ^(.*)-(.*)-(.*)-(.*).html script.php?a=$1&b=$2-$3-$4
RewriteRule ^(.*)-(.*)-(.*).html script.php?a=$1&b=$2-$3

and so on?

It seems I can't do non-greedy matching as I get an error if I put a? in there.

Any suggestions?

jdMorgan

1:23 am on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Bonanza,

Tell the regex parser to grab any characters which are not "-" within each group.

Whether you need to have four separate rules depends on what your script will accept in the case of missing parameters. If the script will accept "?a=A&b=&c=&d=", then you only need the first RewriteRule below. If it won't accept the "blank" parameters, then use all four.


RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4
RewriteRule ^([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3
RewriteRule ^([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2
RewriteRule ^([^-]*)\.html$ /script.php?a=$1

Reference - See links to regex tutorial in this post: Introduction to mod_rewrite [webmasterworld.com]

HTH,
Jim

bonanza

2:05 am on Apr 28, 2003 (gmt 0)



Excellent, Jim, thanks.

I only needed the first. Empty parms are no problem.

At risk of wearing out my welcome, I've already got a part two.

I want to be able to handle an optional querystring.

/widget-A-B-C-D.html?p=1

Here's what I tried after reading up on it a bit:

RewriteCond %{query_string} p=([^&]+)
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4&p=%1
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4

but that doesn't seem to work.

Thanks again for your help.

bonanza

2:36 am on Apr 28, 2003 (gmt 0)



ok, sorry, the single line didn't work. I was faked out because I'm testing in a subdirectory and the wrong directory got called.

I had to put in a line for each possible number of parameters, like you documented.

And to get the querystring, I tacked &%{query_string} on the end of each.


RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4&%{query_string}
RewriteRule ^([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&%{query_string}
RewriteRule ^([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&%{query_string}
RewriteRule ^([^-]*)\.html$ /script.php?a=$1&%{query_string}

The only possible issue is that if there is no querystring, I'll be putting an extra & on the end. That shouldn't cause a problem, I hope.

Unless you see something wrong with what I'm doing here, I think I'm set. thanks.

jdMorgan

3:05 am on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



bonanza,


RewriteCond %{query_string} p=([^&]+)
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4&p=%1
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4

but that doesn't seem to work.

Can you be more specific about how it didn't work? Barring any character escaping problems, it looks OK.

Jim

bonanza

3:12 am on Apr 28, 2003 (gmt 0)



It's hard to debug this stuff when all you get is 404 errors, but I think the problem was this.

With just the single line intended to handle all cases:

RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /script.php?a=$1&b=$2&c=$3&d=$4 

That wouldn't match anything but a URL with 4 parameters in it (or three dashes).

So the above would match this
/widget-A-B-C.html
but not this
/widget-A-B.html or /widget-A.html

So I put in 4 rewrite statments to handle all cases of one parameter to 4 parameters (in addition to widget).

Make sense?

jdMorgan

3:20 am on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



bonanza,

Can you access your site error log? Does it have error entries from mod_rewrite? Sometimes that will give a hint.

I thought you were saying the problem was with your RewriteCond, which looked OK, unles the ampersand needed to be escaped (I can't recall ever trying that myself).

Also note that since your RewriteRules are mutually-exclusive, you can add an [L] flag to speed things up by stopping the rewrite engine as soon as the first rule matches and the rewrite is done.

Jim

bonanza

3:34 am on Apr 28, 2003 (gmt 0)



Yes, I can see the error log and it was getting 404 errors, I think because the single line just wasn't matching anything that didn't have 4 values in it.

When I was trying to make the regexp non-greedy with a (.*?), I was getting better errors. ;)

I think the RewriteCond problem was a red herring. I copied my site to a subdir to test and some of the links pointed me back to the root dir where I had things working otherwise and had me confused for a while.

So the attempt with the RewriteCond probably worked, but only for a url with 4 parameters in it.

Good point on the [L]'s. I'll add those.

jdMorgan

3:47 am on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



bonanza,

Yes, the RewriteCond is only good for the first RewriteRule which follows it. It does not apply to subsequent RewriteRules, so the %1 backreference became undefined for the second, third, and fourth RewriteRule - in addition to the fact that these subsequent rules would be processed unConditionally... :o

Interesting about your error log - my hosted accounts are set up to write (at least the most serious) mod_rewrite errors to the same log file as 404 errors.

Jim

bonanza

4:01 am on Apr 28, 2003 (gmt 0)



I get errors in my error log as well, but in this case there simply weren't any errors. Just no match to the regexps.

thanks for your help. I'm cruisin' now. Have successfully added a few more rewrites for a few other unrelated things as well.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month