HTACCESS not reading one variable - QSA or L?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

HTACCESS not reading one variable - QSA or L?

Post by simonmlewis »

Code: Select all

DirectoryIndex index.php index.html index.htm
order allow,deny
allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

RewriteRule ^(blog)($|/) - [L]

RewriteRule ^pricedrop/page/([0-9]+)/ /index.php?page=pricedrop&pagenum=$1 [QSA]
RewriteRule ^productsnew/page/([0-9]+)/ /index.php?page=productsnew&pagenum=$1 [QSA]


RewriteRule ^categ/([0-9]+)/([^/]+) /index.php?page=categ&c=$1&cname=$2 [QSA]
RewriteRule ^categ/page/([0-9]+)/([^/]+)/([0-9]+) /index.php?page=categ&c=$1&cname=$2&pagenum=$3 [QSA]
RewriteRule ^subcateg/([0-9]+)/([^/]+)/([0-9]+)/([^/]+) /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub [QSA]
RewriteRule ^subcateg/page/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+) /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&pagenum=$5 [QSA]

RewriteRule ^product/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+)/([^/]+) /index.php?page=product&c=$1&cname=$2&s=$3&sname=$4&product=$5&h=$6 [QSA]

RewriteRule ^romancode/([^/]+) /index.php?page=romancode&romancode=$1 [QSA]
RewriteRule ^productsall/([0-9]+)/ /index.php?page=productsall/ [QSA]

RewriteRule ^productsall/page/([0-9]+)/ /index.php?page=productsall&pagenum=$1/ [QSA]
RewriteRule ^manufacturers/([^/]+) /index.php?page=manufacturers&manufacturer=$1 [QSA]

RewriteRule ^type-of-product/([^/]+) /index.php?page=type-of-product&producttype=$1 [QSA]
RewriteRule ^type-of-product/([^/]+)/page/([0-9]+) /index.php?page=type-of-product&producttype=$1&pagenum=$2 [L]

RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [QSA]

RewriteRule ^$ index.php?page=home [QSA]

RewriteRule ^robots.txt$ robots.php [QSA]
<a href=\"/type-of-product/$producttype/page/$page/\" class='pagelink'>[Next]</a>

This link isn't working. It opens the page, but it doesn't see it as page 2, it only sees it as Page one.
Even if I put code into the top to see what page it sees, it's always one. I've tried the page line wtih QSA, but it doesn't work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS not reading one variable - QSA or L?

Post by Celauran »

Take a look at http://htaccess.madewithlove.be/

Reversing the order of your two type-of-product rules should do the trick
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS not reading one variable - QSA or L?

Post by simonmlewis »

OH yes. I don't fully understand the QSA and L. And certainly don't understand why changing the order fixes it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS not reading one variable - QSA or L?

Post by Celauran »

QSA = Query String Append. If you're rewriting /foo/bar?stuff=baz, you need QSA to keep ?stuff=baz after the rewrite.
L = Last. Further rules will not be applied.

The reason the order matters is because of the regular expressions being used. More specific rules need to come first to avoid getting caught by less specific rules. In your case, /type-of-product/foo/page/2 matches
[text]RewriteRule ^type-of-product/([^/]+) /index.php?page=type-of-product&producttype=$1 [QSA][/text]
gets rewritten to /index.php?page=type-of-product&producttype=foo, which then does not match the second rule. Reversing the order means it matches the intended rewrite rule, gets rewritten, and further rewrite attempts are halted due to the L instruction.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS not reading one variable - QSA or L?

Post by simonmlewis »

Gotcha.
So "extra" rules that are part of a basic rule, should come before the basic rule, for that very reason (to avoid it ignore them).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS not reading one variable - QSA or L?

Post by Celauran »

Yes, precisely. More complex, more specific rules first, broader rules next, catch-all rules last.
Post Reply