Replace "name" parameter from query in wordpress

361 Views Asked by At

My new wordpress site has to receive and then redirect a GET request from outside that has a name parameter appended in the request. "Name" is a reserved parameter in wordpress so when the request is received i get a 404.

old request: www_myweb_com/old_purchase?name=Michael&lastname=Smith
new address: www_myweb_com/new_purchase?firstname=Michael&lastname=Smith

The redirect is already solved with a plugin, but the big deal is how to replace that name parameter to firstname. Can anyone help me with this?
Thank you in advance.

1

There are 1 best solutions below

0
Prid On

Find .htaccess file at the root of your Wordpress directory, open it and add this AT THE TOP (above the pre-existing Wordpress rules):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/old_purchase$
RewriteCond %{QUERY_STRING} ^name=(.*)&(.*) [NC]
RewriteRule (.*) /new_purchase?firstname=%1&%2 [NE,QSD,R,L]
</IfModule>

What this does:

  1. Checks if the URL matches www.myweb.com/old_purchase
  2. Finds the regex pattern ^name(.*)&(.*) among the URL query
    • (e.g. www.myweb.com/old_purchase?name=...&lastname=...&others.. matches this regex, and stores the name value as a variable, and the rest of the query parameters as well)
  3. Redirects to www.myweb.com/new_purchase?firstname=%1&%2 where %1 is the name value stored earlier, and %2 are the rest of the query params
    • The full redirected URL then becomes www.myweb.com/new_purchase?firstname=...&lastname=...&others..

NOTE: I'm a n00b at Apache and htaccess syntax and language, so this may not be the best solution. But it works :)