RewriteCond to match query string parameters in any order - apache

I have a URL which may contain three parameters:
?category=computers
&subcategory=laptops
&product=dell-inspiron-15
I need 301 redirect this URL to its friendly version:
http://store.example.com/computers/laptops/dell-inspiron-15/
I have this but cannot make it to work if the query string parameters are in any other order:
RewriteCond %{QUERY_STRING} ^category=(\w+)&subcategory=(\w+)&product=(\w+) [NC]
RewriteRule ^index\.php$ http://store.example.com/%1/%2/%3/? [R,L]

You can achieve this with multiple steps, by detecting one parameter and then forwarding to the next step and then redirecting to the final destination
RewriteEngine On
RewriteCond %{QUERY_STRING} ^category=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &category=([^&]+) [NC]
RewriteRule ^index\.php$ $0/%1
RewriteCond %{QUERY_STRING} ^subcategory=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &subcategory=([^&]+) [NC]
RewriteRule ^index\.php/[^/]+$ $0/%1
RewriteCond %{QUERY_STRING} ^product=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &product=([^&]+) [NC]
RewriteRule ^index\.php/([^/]+/[^/]+)$ http://store.example.com/$1/%1/? [R,L]
To avoid the OR and double condition, you can use
RewriteCond %{QUERY_STRING} (?:^|&)category=([^&]+) [NC]
as #TrueBlue suggested.
Another approach is to prefix the TestString QUERY_STRING with an ampersand &, and check always
RewriteCond &%{QUERY_STRING} &category=([^&]+) [NC]
This technique (prefixing the TestString) can also be used to carry forward already found parameters to the next RewriteCond. This lets us simplify the three rules to just one
RewriteCond &%{QUERY_STRING} &category=([^&]+) [NC]
RewriteCond %1!&%{QUERY_STRING} (.+)!.*&subcategory=([^&]+) [NC]
RewriteCond %1/%2!&%{QUERY_STRING} (.+)!.*&product=([^&]+) [NC]
RewriteRule ^index\.php$ http://store.example.com/%1/%2/? [R,L]
The ! is only used to separate the already found and reordered parameters from the QUERY_STRING.

I take a slightly different approach for this sort of thing, leveraging ENV VARs set and read by mod_rewrite. I find it more readable / maintainable to refer to the backreferences by name like this, and these ENV VARs can be reused later in request processing too. Overall I think it's a more powerful and flexible approach than the accepted answer here. In any case, it works well for me. I've copied my gist below in its entirety:
From https://gist.github.com/cweekly/5ee064ddd551e1997d4c
# Mod_rewrite is great at manipulating HTTP requests.
# Using it to set and read temp env vars is a helpful technique.
#
# This example walks through fixing a query string:
# Extract good query params, discard unwanted ones, reorder good ones, append one new one.
#
# Before: /before?badparam=here&baz=w00t&foo=1&bar=good&mood=bad
# After: /after?foo=1&bar=good&baz=w00t&mood=happy
#
# Storing parts of the request (or anything you want to insert into it) in ENV VARs is convenient.
# Note the special RewriteRule target of "-" which means "no redirect; simply apply side effects"
# This lets you manipulate the request at will over multiple steps.
#
# In a RewriteRule, set custom temp ENV VARs via [E=NAME:value]
# Note it's also possible to set multiple env vars
# like [E=VAR_ONE:hi,E=VAR_TWO:bye]
#
# You can read these values using %{ENV:VAR_NAME}e <- little "e" is not a typo
#
# Tangent:
# Note you can also read these env vars the same way, if you set them via SetEnvIf[NoCase]
# (It won't work to use SetEnv, which runs too early for mod_rewrite to pair with it.)
#
# Regex details:
# (?:) syntax means "match but don't store group in %1 backreference"
# so (?:^|&) is simply the ^ beginning or an & delimiter
# (the only 2 possibilities for the start of a qs param)
# ([^&]+) means 1 or more chars that are not an & delimiter
RewriteCond %{QUERY_STRING} (?:^|&)foo=([^&]+)
RewriteRule ^/before - [E=FOO_VAL:%1]
RewriteCond %{QUERY_STRING} (?:^|&)bar=([^&]+)
RewriteRule ^/before - [E=BAR_VAL:%1]
RewriteCond %{QUERY_STRING} (?:^|&)baz=([^&]+)
RewriteRule ^/before - [E=BAZ_VAL:%1]
RewriteRule ^/before /after?foo=%{FOO_VAL}e&bar=%{BAR_VAL}e&baz=%{BAZ_VAL}e&mood=happy [R=301,L]
P.S. This is not a copy/pasteable solution to your question, but rather shows exactly how to handle this kind of problem. Armed w this understanding, leveraging it for your example will be completely trivial. :)

1) In case You just need to check that all parameters are in url:
RewriteCond %{QUERY_STRING} (^|&)category\=computers($|&)
RewriteCond %{QUERY_STRING} (^|&)subcategory\=laptops($|&)
RewriteCond %{QUERY_STRING} (^|&)product\=dell\-inspiron\-15($|&)
RewriteRule ^$ http://store.example.com/computers/laptops/dell-inspiron-15/? [R=301,L]
2) In case You need exact set of parameters:
RewriteCond %{QUERY_STRING} ^&*(?:category\=computers|subcategory\=laptops|product\=dell\-inspiron\-15)(?!.*&\1(?:&|$))(?:&+(category\=computers|subcategory\=laptops|product\=dell\-inspiron\-15)(?!.*&\1(?:&|$))){2}&*$
RewriteRule ^$ http://store.example.com/computers/laptops/dell-inspiron-15/? [R=301,L]
This rule is generated by 301 redirect generator

Related

Rewrite Apache Query_String

I have a query string URL: http://localhost/cgi-bin/qgis_mapserv.fcgi?map=/home/qgis/project/map.qgs. I want to hind the map.qgs path in the variable MAP. Besides the map variable, there are some variables (version, request, service, etc).
Here is what I need:
RewriteRule:
Pattern: ^cgi-bin/qgis_mapserv.fcgi?map=map.qgs&(.)$
Substitution: ^cgi-bin/qgis_mapserv.fcgi?map=/home/qgis/project/map.qgs&(.)$
Find bellow my unsuccessful attempt:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgi-bin/qgis_mapserv.fcgi$
RewriteCond %{QUERY_STRING} ^map=([A-Za-z0-9.-_]+)$
RewriteRule ^cgi-bin/qgis_mapserv.fcgi?(.*)$ cgi-bin/qgis_mapserv.fcgi?$1
Note: The map variable can show up in the pattern uRL anywhere among the other variables.
I wonder what I am missing on the code above
You are almost there.
The reason why your rule isn't working is because you can't test queryString in pattern of a RewriteRule.
You need to change your rule's pattern to
RewriteRule ^cgi-bin/qgis_mapserv.fcgi$
With this change your htaccess rules will look like :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgi-bin/qgis_mapserv.fcgi$
RewriteCond %{QUERY_STRING} ^map=([A-Za-z0-9./-_]+)/map.qgs$
RewriteRule ^/?cgi-bin/qgis_mapserv.fcgi$ /cgi-bin/qgis_mapserv.fcgi?%1 [R,L]

Combination of rewrites in .htaccess doesn't work

I have different rules in my .htaccess file which work fine individually but combined in one file they don't.
Here are some examples of my file:
# take care of %C2%A0
RewriteRule ^(.+)\xc2\xa0(.+)$ $1-$2 [L,NE]
# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
# executes when there is exactly 1 space in URI
RewriteRule "^productdetails/617/6/(\S*) (\S*?)/?$" /$1-$2/302 [L,R=302,NE]
Also I've got the following:
RewriteCond %{QUERY_STRING} ^pid=617/?$
RewriteRule ^productdetails\.asp$ /Casio-CDP120-Digital-Piano-in-Black/302? [L,NC,R=301]
which still work fine.
I have now added the following:
RewriteRule "^categories/3/Kawai Digital Pianos/?$" /Compare/Kawai-Digital-Pianos [L,NC,R=301]
which used to rewrite:
mysite.co.uk/categories/3/Kawai%20Digital%20Pianos/ to mysite.co.uk/Compare/Kawai-Digital-Pianos
this does not work anymore
Any help to get the last rule working in combination with the others would be great
You just need to make sure order of rules is correct. For your examples following order should work:
RewriteRule "^categories/3/Kawai Digital Pianos/?$" /Compare/Kawai-Digital-Pianos [L,NC,R=301]
RewriteCond %{QUERY_STRING} ^pid=617/?$
RewriteRule ^productdetails\.asp$ /Casio-CDP120-Digital-Piano-in-Black/302? [L,NC,R=301]
# take care of %C2%A0
RewriteRule ^(.+)\xc2\xa0(.+)$ $1-$2 [L,NE]
# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
# executes when there is exactly 1 space in URI
RewriteRule "^productdetails/617/6/(\S*) (\S*?)/?$" /$1-$2/302 [L,R=301,NE]

htaccess check length of a part of a query string

I want apache to skip certain rewrites in case part of a request is shorter than 255 characters (has to do with caching and the 255 character filename limit in linux).
I've written this:
RewriteCond %{QUERY_STRING} "utm_campaign"
RewriteCond %{QUERY_STRING} "utm_medium"
RewriteCond %{QUERY_STRING} ^(.*\/)([^\/\n]{0,255})$
RewriteRule .* - [S=2]
And I tested the regex against the url (q=path/to/page?utm_campaign=xxx&utm_medium=xxx) and it matches but the query_string variable seems to have a different content because the 2 rules after this still get executed. The part that should match is in this case page?utm_campaign=xxx&utm_medium=xxx (and everything after this) If this is shorter than 255 characters the next 2 rewrite rules can be skipped.
I'm using Drupal 6 btw.
The part before the ? is not in %{QUERY_STRING} (contrary to $_SERVER['QUERY_STRING'] in PHP in this case, hence the confusion), adding %{REQUEST_URI} to the RewriteCond solved the problem:
RewriteCond %{QUERY_STRING} "utm_campaign"
RewriteCond %{REQUEST_URI}%_{QUERY_STRING} "utm_medium"
RewriteCond %{QUERY_STRING} ^(.*\/)([^\/\n]{0,255})$
RewriteRule .* - [S=2]
Not sure how to give #Kamil Šrot credit for this solution since the answer is in a comment?

apache mod_rewrite and number of query parameter

Here I have an original url:
/index.php?controller=controller_name&action=action_name&param1=val1&param2=val2
I need a rule that will transform it to:
/controller_name/action_name/param1/val1/param2/val2/
The issue is I don't know how many param out there, param may have any name, for example a real case:
/member/filter/location/us/gender/male/age/20
Thanks for your help!
RewriteEngine On
# match query string in the form
# controller=controller_name&action=action_name&param1=val1&param2=val2
# and rewrite it to /controller_name/action_name/?param1=val1&param2=val2
RewriteCond %{QUERY_STRING} ^controller=([^&]+)&action=([^&]+)&?(.*)$
RewriteRule .* /%1/%2?%3
# parse the query string from result of rules above
# and create "directories" from key, value pairs
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} ^&?([^=]+)=([^&]+)&?(.*)$
RewriteRule ^(.*)$ $1/%1/%2?%3

Ignore requests from internal redirects

RewriteRule ^resources/.+$ - [L]
RewriteRule .? index.php?t=$0 [QSA,L]
Would produce a 500 - Internal Server Error, because it would repeat again and again the same rule, due to internal redirected requests which are exactly treated as the first one. It would lead to an infinite chain of index.php?t=index.php&t=index.php&t=index.php&[...infinite more...]&t=test.php
But in my opinion this is not much better:
RewriteRule ^resources/.+$ - [L]
RewriteCond %{QUERY_STRING} !t=
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule .? index.php?t=$0 [QSA,L]
Because now the user could input index.php?t=test.php as address, would pass the script and get the same content as if he had given test.php. I don't like that.
So how do I execute the first one without the issue of repeating internal redirects? Surely, a flag VL - Very Last would do the trick but sadly it does not exist.
First we have a look at all parameters given to the rules possibly indicating whether this is a chained request or not. This means, we either 1) need a variable changed in chained requests not relative to the changed URI or 2) the opposite, a variable which is relative to the changed URI and did not change (because we can compare it then against the others who did chage).
The problem is, they almost all update according to the applied RewriteRules.
IS_SUBREQ (1) and THE_REQUEST (2) are the only interesting variables but sadly internal redirects are not treated as subrequests, so IS_SUBREQ disappears. Only THE_REQUEST does not change and contains the real given path, so we have found our entry point.
With this in mind here is the annoying complex solution:
RewriteEngine On
# Set SCRIPT_URI and SUBREQ
# MUST be the first statements in the file
# SCRIPT_URI is the original browser-requested path
# SUBREQ is "true" if the original browser-requested path is not overriden yet
RewriteCond %{ENV:REQUEST_PARSED} !true
RewriteCond %{THE_REQUEST} ^\s*\w+\s+(http://[^\s/]+/|/?)([^\s\?]*)[\s\?$]
RewriteRule .? - [E=SCRIPT_URI:/%2,C]
RewriteRule .? - [E=REQUEST_PARSED:true]
RewriteCond %{ENV:SCRIPT_URI} ^(.*?)/\.($|/.*$)
RewriteRule .? - [E=SCRIPT_URI:%1%2,N]
RewriteCond %{ENV:SCRIPT_URI} ^(.*?)/[^/]+/\.\.($|/.*$)
RewriteRule .? - [E=SCRIPT_URI:%1%3,N]
RewriteCond %{ENV:SCRIPT_URI} ^(.*?)//\.\.($|/.*$)
RewriteRule .? - [E=SCRIPT_URI:%1/%2,N]
RewriteCond %{ENV:SCRIPT_URI}#%{REQUEST_URI} !^/*(.*)#/*\1$
RewriteRule .? - [E=SUBREQ:true]
# SCRIPT_URI and SUBREQ are set now. Actual content follows:
RewriteCond %{ENV:SUBREQ} !true
RewriteRule ^resources/.+$ - [L]
RewriteCond %{ENV:SUBREQ} !true
RewriteRule .? index.php?t=$0 [QSA,L]