Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Chained rewriterule problem with last parameters



5:53 pm on Aug 17, 2007 (gmt 0)

5+ Year Member


I cant seem to get these two rules to work together.

The first is the actual page and the second is the paginated page.

rewriterule ^([a-zA-Z0-9_/-]+)/([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/$ script.php.$9?sec=$1&cat=$2&geta=$3&geturla=$4&getb=$5&geturlb=$6&getc=$7&geturlc=$8 [C]
rewriterule ^script\.php\.([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/$ /testtd/index.php?var=$1&getd=$2&geturld=$3&gete=$4&geturle=$5 [QSA,L]

rewriterule ^([a-zA-Z0-9_/-]+)/([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/$ script.php.$9?sec=$1&cat=$2&geta=$3&geturla=$4&getb=$5&geturlb=$6&getc=$7&geturlc=$8 [C]
rewriterule ^script\.php\.([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/([a-zA-Z0-9]+)-([a-zA-Z0-9_/-]+)/_([0-9]+)/$ /testtd/index.php?var=$1&getd=$2&geturld=$3&gete=$4&geturle=$5&page=$6 [QSA,L]

Is it because the first part in the chain, the condition is met and therefore only the 1st rule works?




6:15 pm on Aug 17, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

If you have two rules: one for a specific condition, and another for all the others, then the specific one goes first.


6:40 pm on Aug 17, 2007 (gmt 0)

5+ Year Member

ive tried changing the order and it doesnt work

I felt it might have something to do with the first part in the chain condition being met in the first rule and therefore the first rule applying to both sets of pages, no?

I am wondering if it is possible to use a rewritecond to check if a parameter or character (in my case an underscore) exists in the query string and then append &page= to the end of the rewrite rule. This would achieve what I want,however im not sure how to write it.

Any help is much appreciated




7:21 pm on Aug 17, 2007 (gmt 0)

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

You cannot include "/" within the [a-z0-9_-] alternate character groups used to match your parameters without fatally confusing the regex parser. If you do, the first rule of both rule sets will always match, but the second rule will never match, and therefore both chained sets will fail as a whole. This is because the "+" quantifier is greedy, and each subpattern will match as many characters as possible, leaving the second rule to 'starve' in both cases.

Generalizing, you cannot use a character as a substring separator while also using it inside the substring to be separated.

You also have too many parentheses in the the first rule of both chained sets. You may use nine pairs, and no more. Use the ninth pair to grab *everything* that won't fit in the first eight pairs.

You can make good use of the [NC] flag to simplify your patterns, and make them somewhat more tolerable to read and interpret for those brave souls who care to undertake the task... The [NC] flag makes the pattern-matching case-insensitive, which means you can use just [A-Z] or [a-z] in each of the subpatterns, instead of having to use [A-Za-z] in each one.

Cleaning up the separator/parameter and too-many-parentheses problems, adding the [NC] flag, putting the rules in order, and tidying-up in general, I'd recommend something like this:

# paged
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+-[a-z0-9_-]+/[a-z0-9]+-[a-z0-9_-]+)/$ tempurl/$9?sec=$1&cat=$2&geta=$3&geturla=$4&getb=$5&geturlb=$6&getc=$7&geturlc=$8 [NC,C]
RewriteRule ^tempurl/([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/_([0-9]+)$ /testtd/index.php?var=$1&getd=$2&geturld=$3&gete=$4&geturle=$5&page=$6 [NC,QSA,L]
# non-paged
^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+)-([a-z0-9_-]+)/([a-z0-9]+-[a-z0-9_-]+/[a-z0-9]+-[a-z0-9_-]+)/$ tempurl/$9?sec=$1&cat=$2&geta=$3&geturla=$4&getb=$5&geturlb=$6&getc=$7&geturlc=$8 [NC,C]
RewriteRule ^tempurl/([a-z0-9_/-]+)/([a-z0-9]+)-([a-z0-9_/-]+)/([a-z0-9]+)-([a-z0-9_/-]+)$ /testtd/index.php?var=$1&getd=$2&geturld=$3&gete=$4&geturle=$5 [NC,QSA,L]

Note that since the "filename" created by the first chained rule is temporary and does not exist as a real file (we hope), I have given it a name that implies that it's just an intermediate step, and not a real URL-path.

I may have typoed or fat-finger-deleted something unintentionally. Check carefully and test thoroughly.



Featured Threads

Hot Threads This Week

Hot Threads This Month