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
To avoid the
ORand double condition, you can useas @TrueBlue suggested.
Another approach is to prefix the TestString
QUERY_STRINGwith an ampersand&, and check alwaysThis 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 oneThe
!is only used to separate the already found and reordered parameters from theQUERY_STRING.