Forum Moderators: phranque
# query variables are showing up in mixed order, this is just one possibility
http://www.example.com/gallery.php?gallery_name=the_gallery_name&image_name=Feature_Image_aeIou121qX?sort=most_viewed&page=120&ipp=9&id=2374803&featuring=Old_MacDonald&loc=500&sort=added_last&floc=3&loc=7
http://www.example.com/The_Gallery_Name/feature_image_aeIou121qX?sort=most_viewed&page=120 http://www.example.com/The_Gallery_Name?sort=most_viewed&page=120
# How to grab name/value pairs out of order?
# How to fix stray ? character in middle of query string
RewriteCode ${QUERY_STRING} gallery_name=(\w+)[&]?image_name=(\w+)[&]?page=(\d+)[&]?sort=(\w+) [NC]
RewriteMap uppercase int:toupper
RewriteMap lowercase int:tolower
RewriteRule Transform gallery_name to name case using RewriteMap
RewriteRule ^/gallery.php(.*) /%1/%2(image_name, if present) [QSA}
RewriteCond %{QUERY_STRING} (badparam|otherbadparam|thirdbadparam)
RewriteRule \.php /fixup.php [L] Ideally, the canonical version would look like either:
http://www.example.com/The_Gallery_Name/feature_image_aeIou121qX?sort=most_viewed&page=120
or if there's no image_name query var,
http://www.example.com/The_Gallery_Name?sort=most_viewed&page=120
RewriteCond %{QUERY_STRING} (badparam|otherbadparam|thirdbadparam)
RewriteRule \.php /fixup.php [L]
The php script will use a default sort order without the inclusion of the sort variable, so it's optional and ok if nonexistent.
The_Gallery_Name
most_viewed
so stripping out bad/unwanted query parameters is not possible with RewriteCond / RewriteRule?
RewriteCond %{QUERY_STRING} (?:bad1|bad2|bad3|bad4)
RewriteCond %{QUERY_STRING} (gallery_name=[^&]*)
RewriteCond %{QUERY_STRING} (sort=[^&]*)
RewriteCond %{QUERY_STRING} (image_name=[^&]*)?
RewriteCond %{QUERY_STRING} (page=[^&]*)?
RewriteRule ^(.+\.php) http://www.example.com/$1?%1&%2&%3&%4 [R=301,L]
so you're saying these 2 urls will generate the same response?
http://www.example.com/The_Gallery_Name?sort=most_viewed&page=120
http://www.example.com/The_Gallery_Name?page=120
in that case the canonical url should exclude the sort parameter since it has a default value.
The_Gallery_Name
most_viewed
uggh!
if it's not "too late", i would recommend folding that to lower case and using hyphens instead of underscores.
All things are possible with mod_rewrite ;) but sometimes it's just more trouble than it's worth. That's why I asked how many bad parameters there are, and how many possible sort orders.
The one thing you can NOT do is:
RewriteCond %{QUERY_STRING} (?:bad1|bad2|bad3|bad4)
RewriteCond %{QUERY_STRING} (gallery_name=[^&]*)
RewriteCond %{QUERY_STRING} (sort=[^&]*)
RewriteCond %{QUERY_STRING} (image_name=[^&]*)?
RewriteCond %{QUERY_STRING} (page=[^&]*)?
RewriteRule ^(.+\.php) http://www.example.com/$1?%1&%2&%3&%4 [R=301,L]
Again, you can NOT do this, because mod_rewrite does not allow captures from more than one Condition. (Multiple captures from a single Condition, yes, provided it's the last one.) So you'd have to write a rule for every possible permutation, with a separate ruleset for each. Four parameters, variable order = 4! = 24 rulesets.
But you could easily do this with a few lines of php.