Score:2

Redirect part of a URL parameter to the URL-Path

id flag

I used a module that created links like this:

https://example.com/login?create_account=1&back=https://example.com/product/2164/?ets_rv_add_review=1

I need to redirect them to:

https://example.com/product/2164/

There are hundreds of these links.

I tried this but it's not working:

RewriteCond %{QUERY_STRING} ^create_account=([0-9]+)&back=([0-9]+)$
RewriteRule /login https://example.com/product/%1/? [L,R=301]
Score:2
kz flag
RewriteCond %{QUERY_STRING} ^create_account=([0-9]+)&back=([0-9]+)$
RewriteRule /login https://example.com/product/%1/? [L,R=301]

This tries to match a back URL parameter whose value consists solely of numbers, but your example includes an absolute URL (numbers are only in the last path segment). There are also more URL parameters in your example. /login would also fail to match in a .htaccess context since the URL-path matched by the RewriteRule pattern does not start with a slash.

Try the following instead, near the top of the root .htaccess file (the order is important):

RewriteCond %{QUERY_STRING} ^create_account=\d+&back=https://[^/]+/([^?]+)
RewriteRule ^login$ https://example.com/%1 [QSD,R=301,L]

I've included the product from the requested URL in the captured backreference (%1). This captures everything up to the query string.

And always test first with a 302 (temporary) redirect to avoid potential caching issues.

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.