1

I need help to redirect URLs with multiple language codes to one with .htaccess. The following URLs should redirect:

  • https://example.com/cp/cart.php?a=add&pid=7&language=us

  • https://example.com/cp/cart.php?a=add&pid=5&language=de

  • https://example.com/cp/cart.php?language=nl

  • https://example.com/cp/home.php?language=uk&test=a

to

  • https://example.com/cp/cart.php?a=add&pid=7&language=portuguese-pt

  • https://example.com/cp/cart.php?a=add&pid=5&language=portuguese-pt

  • https://example.com/cp/cart.php?language=portuguese-pt

  • https://example.com/cp/home.php?language=portuguese-pt&test=a

IMPORTENT : Do not do anything if the URL does not contain a "language" URL parameter.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • What have you tried? Where is your `.htaccess` file located? In the document root or the `cp` subdirectory? If not in `cp` are you OK with creating another `.htaccess` file in that subdirectory? – MrWhite Oct 09 '22 at 16:35
  • Does this rule only apply to URL-paths `/cp/cart.php` and `/cp/home.php`? Or could this apply to any URL-path? – MrWhite Oct 09 '22 at 17:37
  • 1
    @MrWhite my .htaccess file path is in cp folder . and i have alot file like home.php and cart.php. i need this for any url – Pouyan Danesh Oct 09 '22 at 17:59

1 Answers1

0

my .htaccess file path is in cp folder. and i have alot file like home.php and cart.php. i need this for any url

To act on any URL-path then you could do something like the following near the top of the /cp/.htaccess file using mod_rewrite:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^(.*&)?(language=)(?!portuguese-pt(?:&|$))[^&]*(&.*)?$
RewriteRule ^ %{REQUEST_URI}?%1%2portuguese-pt%3 [NE,R=302,L]

The RewriteRule pattern ^ is successful for any URL-path.

The preceding condition (RewriteCond directive) matches the query string. It is only successful when the language URL parameter exists, but not equal to portuguese-pt (as determined by the negative-lookahead). The value of the language URL param is otherwise discarded.

We capture the parts of the query string that surround the language URL parameter value. These are saved in the following backreferences:

  • %1 / pattern (.*&)? - the part of the query string before the language URL parameter (if any). This includes the & delimiter before the language URL param.
  • %2 / pattern (language=) - captures the literal text language=. This must match to be successful. This simply saves repetition later.
  • %3 / pattern (&.*)? - the part of the query string after the language URL parameter (if any). This includes the & delimiter after the language URL param.

The REQUEST_URI server variable in the substitution string contains the full root-relative URL-path. We follow this with the query string that we rebuild using the backreferences as explained above, injecting portuguese-pt as the value of the language URL param (replacing any other value this URL parameter might have had).

The NE (noescape) flag is required to prevent the & (URL param delimiter) being URL-encoded in the response.

Test first with a 302 (temporary) redirect to avoid potential caching issues, even if you change this to a permanent redirect later.

MrWhite
  • 12,647
  • 4
  • 29
  • 41