mod_rewrite variable forwarding

Right now i have my rules so /page/products.html get rewritten to index.php?page=products

What i need is to pass variables so when /page/products.html?pg=4 is called to froward it to index.php?page=products&pg=4

how would my rule look ?

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^/.\.html\?.$
RewriteRule ^(.)\.html\?(.)$ /index.php?pg=$1 [L]

Take a look at the flag QSA in the RewriteRule documentation from Apache:


'qsappend|QSA' (query string append)
This flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.

So your rule would become

RewriteRule ^/?products.html$ /index.php?page=products [L,QSA]

:tup: Exactly!

Note: The query string is automatically passed through UNLESS you create a new query string in the redirection which then makes the QSA necessary. Conversely, to kill a query string, create a new (empty) query string with ? and it will NOT be shown in the URI.

Regards,

DK

I don’t think you got what i need,

Lets say a user goes to site.com/page/products.html

and there 10 pages of products and he clicks "next page"

Then it takes him to site.com/page/products.html?page=2

I would like to also pass category as well…
Pretty much i need to pass variables to index.php

or i would have to redo all my links and make it like

site.com/sunrooms/wood/10 to redirect to index.php?page=sunrooms&cat=wood&p=10

but i also need to go to sunrooms main page if the link is just site.com/sunrooms

I pretty much need a way of pagination… and all my rewrite rules right now are /page/productname.html so i have no way of pasing what page was requested to php…

info,

Okay, let me try to restate your “specifications,” if I can:

URIs:

sunrooms/wood/10 to index.php?page=sunrooms&cat=wood&p=10
page/products.html to index.php?page=products

Clearly, there are two formats for your URIs to be using so you need two mod_rewrite block statements to deal with them but do NOT fall into the trap of using (.*) as this garbage collector (lazy regex) will match nothing and EVERYTHING!

If you can ensure that your links are of the forms on the left above, the main difference between the two is the number of pseudo directory levels and the lack of the html extension has been replaced with digits:

RewriteEngine on
# sunrooms/wood/10 to index.php?page=sunrooms&cat=wood&p=10
RewriteRule ^([a-z]+)/([a-z]+)/([0-9]+)$ index.php?page=$1&cat=$2&p=$3 [L]

# page/products.html to index.php?page=products
[I]# What's the page for in the from URI?[/I]
RewriteRule ^([a-z]+)/([a-z]+)\\.html$ index.php?page=$2 [L]

Of course, I’ve required your page and cat to consist only of lowercase characters and the p value to be digits. You can also eliminate the page requirement by making the third atom optional as follows:

^([a-z]+)/([a-z]+)[COLOR="Red"]([/COLOR]/([0-9]+)[COLOR="Red"])?[/COLOR]$

Noone will see the empty value for p if it’s missing in the URI but it will not require it to be present.

The point that I’m trying to make is that creating a “specification” before you start your mod_rewrite coding should make the task abundantly clear which makes the coding a trivial exercise.

Regards,

DK