Score:1

.htaccess redirect if param value not portuguese-pt (FORCE WHMCS ONLY ONE LANG)

eg flag

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.

kz flag
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?
kz flag
Does this rule only apply to URL-paths `/cp/cart.php` and `/cp/home.php`? Or could this apply to any URL-path?
Pouyan Danesh avatar
eg flag
@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
Score:0
kz flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.