2

I am trying to convert my nginx blocks to apache(.htaccess) file but not able to achive it. Can you please help me with this

location = /index.php {
  if ($arg_a = get) {
    return 301 /$arg_a/$arg_b;
  }
}

location = /home.php {
 return 301 /$arg_bool;
}

for the action = get, here is what the legacy website url looked like:

/index.php?action=get&id=5000849

(the code above rewrites to/get/5000849)

Kalpit
  • 121
  • 4

1 Answers1

1

(the code above rewrites to/get/5000849)

The above Nginx code is an external (301) redirect, not strictly a "rewrite". (Although you will most probably need a rewrite as well later in your .htaccess file.)

To redirect /index.php?action=get&id=5000849 to /get/5000849 in .htaccess you would do something like the following near the top of your .htaccess file:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^action=(get)&id=(\d+)
RewriteRule ^index\.php$ /%1/%2 [R=302,L]

The get part could have been hardcoded, instead of using a backreference, since that appears to be constant. Although using a backreference saves repetition (which would be my preference).

The check against the REDIRECT_STATUS environment variable is to avoid a redirect loop, as I assume you already have a directive that rewrites the request back to index.php later in your config file. (?)

%1 and %2 are backreferences to the captured groups in the last matched CondPattern (preceding RewriteCond directive).

This redirect should only be used to redirect an old URL structure in order to maintain SEO. This should not be integral to your site functionality.

NB: Test with 302 (temporary) redirects to avoid caching issues. And only change to 301 (permanent) when you are sure it's working OK. You will need to clear your browser cache before testing.

UPDATE: home.php?bool=contact will redirect to /contact

For the second part, the same principle applies, except there is just one URL parameter instead of two. For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^bool=([a-z]+)
RewriteRule ^home\.php$ /%1 [R=302,L]

This assumes <contact> in your example is always a-z (lowercase) - as in your example. Adjust the regex [a-z]+ as required.

MrWhite
  • 12,647
  • 4
  • 29
  • 41