Forum Moderators: phranque
/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
It seems I can't do non-greedy matching as I get an error if I put a? in there.
Any suggestions?
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
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.
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.
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.
Jim
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?
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
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.
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
thanks for your help. I'm cruisin' now. Have successfully added a few more rewrites for a few other unrelated things as well.