Apache Mod_Rewrite - Appending query strings to rewritten url's

Hello,

I’m struggling with something at the moment. I’ve managed to rewrite the URL for a page displaying products, including pagination, but the page has a product filter which can give up to four extra parameters. The filter is actually a form but the results need to be paginated so I need to add these to the rewritten URL’s. Is there any way to add a query string (?this=1&that=whatever) to the end of a rewritten URL with mod_rewrite.

Hope that makes sense.

Cheers,

Jon

What are your current rewrite rules Jon?

Here are the rules for that page (everything else is under control):



RewriteRule ^products/.*-S([0-9]+)/page-([0-9]+)-of-([0-9]+)$ /index.php?p=products&sub_id=$1&s=$2&np=$3 [NC,L]

RewriteRule ^products/.*-S([0-9]+)$ /index.php?p=products&sub_id=$1 [L]

RewriteRule ^products/.*-S([0-9]+)/all$ /index.php?p=products&sub_id=$1&view=all [NC,L]


<edit>I need between 0 and 4 extra parameters</edit>

Cheers,

Jon

can you give an example of the kind of (full) URI you are trying to match?

Here’s the longest version:

index.php?p=products&sub_id=47&s=2&np=3&show=instore&order=h2l&brand_id=7&mat_id=3

<edit>s and np are for pagination</edit>

Thanks for your help.

Cheers,

Jon

That’s the one you’re rewriting to, what are you rewriting from?

/products/47/2/3/instore/7/3 or something?

I’m not rewriting all those parameters at the moment, and I was hoping to avoid it because most are only shown after HTTP POST, which is why I was hoping to dump them on the end of the rewritten URL as a query string.

When you visit the page without filtering results the URI’s are:

/products/bookcases-S56
/products/bookcases-S56/page-2-of-2
/products/bookcases-S56/all

Cheers,

Jon

ah, in that case you can use:


RewriteCond &#37;{QUERY_STRING} ^(.*)$
RewriteRule ^products/.*-S([0-9]+)$ /index.php?p=products&sub_id=$1&%1 [L]


Note the %1 at the end of the rule. Essentially RewriteCond matches get put into a variable accessed using %.

Brilliant, that’s perfect.

Thanks for your help.

Cheers,

Jon

I’m not sure I completely understand what you want to achieve, but from the looks of it you could also use the QSA (Query String Append) flag instead of an extra RewriteCond, as in :


RewriteRule ^products/.*-S([0-9]+)$ /index.php?p=products&sub_id=$1 [L,QSA]

?

If you are going to do something like that…all you need:

...index.php?...&%{QUERY_STRING}

Otherwise the “QSA” flag is enough.