Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite with extra query string

use QSA? {QUERY_STRING}?

         

LBmtb

8:53 pm on Jun 14, 2007 (gmt 0)

10+ Year Member



I'll get right to it:

Here's the rule:

RewriteRule ^([0-9]+)-[a-zA-Z0-9-]+/([0-9]+)-[a-zA-Z0-9-]+.html$ /MARKETPLACE/category21.php?catid=$2 [L]

An examlple of the url:
205-body-interior/2874-air-bag-suspension-kit.html

I want to be able to have links to
205-body-interior/2874-air-bag-suspension-kit.html?page=2 and have that page variable sent to the PHP page along with the catid (which is working).

So far when entering the URL with the additional "?page=2", it just gets stripped out.

Anybody out there good with mod rewrite? Please?

[edited by: LBmtb at 8:53 pm (utc) on June 14, 2007]

jdMorgan

10:40 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the [QSA] flag -- Query String Append.

Also, for the sake of efficiency, consider using the [NC] flag to make the pattern-matching case-insensitive. This will reduce the time needed to compare the two instances of [a-zA-Z0-9-] by one-third. Also, escape literal periods outside of alternate groups as shown; Unescaped periods mean "match any single character" except inside [alternate-groups].


RewriteRule ^([0-9]+)-[a-z0-9-]+/([0-9]+)-[a-z0-9-]+\.html$ /MARKETPLACE/category21.php?catid=$2 [NC,QSA,L]

You might also be able to use a shorter pattern by indicating what characters should NOT match. For example:

^([^-/]+)-[^-/]+/([^-/]]+)-[^-/.]+\.html$

You can view such negative patterns as "include in the match *until* you find one of these characters." Each of the negative subpatterns needs to contain all of your possible 'field delimiters' so that you can be sure that, for example, the first subpattern won't accept slashes as a match, which would lead to matching longer, deeper-subdirectory, URLs that you don't intend to match. Unless your URLs contain periods for anything but the filetype, only the final subppatern needs to exclude periods. This technique is just something to consider; The direct-match rule above will work fine, just not as fast.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim