Force 302 to 301 redirect

Francis Daly francis at daoine.org
Sat Jan 11 14:20:06 UTC 2020


On Sat, Jan 11, 2020 at 08:35:02AM -0500, tconlon wrote:

Hi there,

> Found it, 
> 
> if ($page == 'index.php') {
>           header("Location: ". $toUrl);
> 
> probably need something like this
> 
> <?php
> // 301 Moved Permanently
> header("Location: ",TRUE,301);

Good that you have found a straightforward solution.

An alternative, which would involve different changes, and would depend
on the actual urls that have been advertised, could be to make a list of
id/city pairs once, and use nginx's "map" to do the translation without
touching the index.php.

Something like, in the "http" block:

    map $request_uri $slug_city {
      /location/index.php?id=235 newyorkcity;
      # more lines like that
    }

and then inside the location that normally handles that request (which
I think is "location ~ \.php$ {"

add

    if ($slug_city) { return 301 /location/$slug_city; }

That may or may not be clearer to whoever is going to maintain the system
in the future.

Cheers,

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list