Force 302 to 301 redirect

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Force 302 to 301 redirect

satay
Hi Team,

I have a funny situation that I can't seem to get around.  Here are the
details.

We had this naming convention to indicate a specific location:
https://myurl/location/index.php?id=235
This naming convention is still out in the internet ether
We changed to go to a slug based operation:
https://myurl/location/newyorkcity
We do the work to determine that id=235 is really the newyorkcity location
in the code
We return back to the client, the correct url
(https://myurl/location/newyorkcity)

When we review the SEO around this old url, we are getting back a 302, that
it's temporary.

how can I tweak the nginx engine to force a 301.

thanks

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286700,286700#msg-286700

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Force 302 to 301 redirect

satay
Can you share the relevant parts of your config?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286700,286701#msg-286701

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Force 302 to 301 redirect

satay
Absolutely..and thanks

Running this on Forge / Digital Ocean

############www.myurl.com

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.myurl.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.myurl.com;
    root /home/forge/www.myurl.com/current/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/www.myurl.com/676408/server.crt;
    ssl_certificate_key /etc/nginx/ssl/www.myurl.com/676408/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers
ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/www.myurl.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

# Expire rules for static content - 2019-05-24
   
    # cache.appcache, your document html and data
    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
        expires -1;
    }
   
    # Feed
    location ~* \.(?:rss|atom)$ {
        expires 1h;
        add_header Pragma public;
        add_header Cache-Control "public";
    }
   
    # Media: images, icons, video, audio, HTC
    location ~*
\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 7d;
        access_log off;
        add_header Pragma public;
        add_header Cache-Control "public";
    }
   
    # CSS and Javascript
    location ~* \.(?:css|js)$ {
        expires 7d;
        access_log off;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/www.myurl.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.myurl.com/after/*;


# Redirect every request to HTTPS...
server {
    listen 80;
    listen [::]:80;

    server_name .myurl.com;
    return 301 https://$host$request_uri;
}



############ssl_redirect.conf

# Redirect SSL to primary domain SSL...
server {
        listen 443 ssl http2;
    listen [::]:443 ssl http2;
   
    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/www.myurl.com/676408/server.crt;
    ssl_certificate_key /etc/nginx/ssl/www.myurl.com/676408/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers
ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    server_name myurl.com;
    return 301 https://www.myurl.com$request_uri;
}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286700,286702#msg-286702

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Force 302 to 301 redirect

Francis Daly
On Fri, Jan 10, 2020 at 03:17:21PM -0500, tconlon wrote:

Hi there,

> Absolutely..and thanks
>
> Running this on Forge / Digital Ocean

I think you indicated that your index.php takes id=235 and decides that
it will return a redirect to https://myurl/location/newyorkcity

Does *that* php code say "send a 301" or "send a 302"?

Can you change it to say "send a 301", if you want a 301?

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Force 302 to 301 redirect

satay
Hi,

Digging into the code will get back to you

Thanks

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286700,286707#msg-286707

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Force 302 to 301 redirect

satay
Hi,

Found it,

if ($page == 'index.php') {
          header("Location: ". $toUrl);

probably need something like this

<?php
// 301 Moved Permanently
header("Location: ",TRUE,301);

thanks

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286700,286708#msg-286708

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Force 302 to 301 redirect

Francis Daly
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        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx