2

I have a situation with opencart where I need to rewrite only the "route" leaving other query strings as it is. Example:

http://www.example.com/?route=module/ext/products&category_id=15&sort=newest

to

http://www.example.com/ext/products?category_id=15&sort=newest

how can I do that?


UPDATE: I have some constraints. This rewrite condition should only be applied to route=module/ecomapi/([^&]+). Not for other routes.

RewriteRule ^ecomapi/21/(products)/([^?]*)$ index.php?route=module/ecomapi/$1&product_id=$2 [L]
RewriteRule ^ecomapi/21/(login)/([^?]*)$ index.php?route=module/ecomapi/$1&email=$2 [L]
RewriteRule ^ecomapi/21/(users)/([^?]*)$ index.php?route=module/ecomapi/$1&id=$2 [L]
RewriteRule ^ecomapi/21/(products|navigation_drawer|banners|cart|login|d‌​evices|shops)?$ index.php?route=module/ecomapi/$1 [B,L,QSA]
2
  • There is a duplicate question on Stack Overflow. stackoverflow.com/questions/11539981/…
    – Steve
    Jan 22, 2017 at 7:05
  • You need to clarify exactly how one URL maps to the other... since your example is doing more than just "rewrite only the "route" leaving other query strings as it is". It looks like you are extracting part of the route URL param (everything after module/?) and using this in the URL-path? Is the route URL param always first in the query string? Is it for this single URL or do you need a general solution? Can you clarify whether you mean a "rewrite" or an "external redirect"?
    – MrWhite
    Jan 22, 2017 at 10:25

1 Answer 1

1

Try the following near the top of your .htaccess file (or server config):

RewriteEngine On
RewriteCond %{QUERY_STRING} ^route=module/([^&]+)&?(.*)$
RewriteRule ^/?$ /%1?%2 [R,L]

This makes the following assumptions:

  • An external redirect (302 - temporary). (Change the R flag to R=301 to make it permanent.)
  • Only an initial request for the document root is considered.
  • The route URL param always exists at the start of the query string.
  • Everything after module/ in the route URL param value is extracted to form the URL-path in the substitution.
  • There must be something after module/. But this "something" could be anything. (You should probably be as explicit as possible here in order to prevent matching against wholly invalid URL-paths.)
  • The route URL parameter could be the only URL param.
3
  • Thanks . This works fine. But I have some constraints. This rewrite condition should be only to "route=module/ecomapi/([^&]+)". Not for other routes RewriteRule ^ecomapi/21/(products)/([^?]*)$ index.php?route=module/ecomapi/$1&product_id=$2 [L] RewriteRule ^ecomapi/21/(login)/([^?]*)$ index.php?route=module/ecomapi/$1&email=$2 [L] RewriteRule ^ecomapi/21/(users)/([^?]*)$ index.php?route=module/ecomapi/$1&id=$2 [L] RewriteRule ^ecomapi/21/(products|navigation_drawer|banners|cart|login|devices|shops)?$ index.php?route=module/ecomapi/$1 [B,L,QSA] Jan 27, 2017 at 1:33
  • But I think there should be a better way to simplify Jan 27, 2017 at 1:38
  • Presumably you've just modified the RewriteCond directive for the route that it should apply to, and that works OK? I don't see how those other directives can be simplified further (or better) - these would seem to be required for your application to work correctly. (?)
    – MrWhite
    Jan 27, 2017 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.