Question

How to 301 Redirect an Old Post to a New Post with Nginx (WordPress)

Posted May 27, 2020 304 views
NginxServer OptimizationUbuntu 16.04

Hi everyone,

Server runs on Ubuntu 16.04.1 x64. Using WordPress. If more info needed, I’d be happy to provide.

I’m trying to redirect an old post to a new one but I’m having a lot of issues. I tried many codes and none worked so far.

Since the server uses Nginx , obviously I can’t use the usual 301 redirect codes that work with Apache.

I used this code a while back to redirect from http to https version for the whole site.

# BEGIN redirecting www to non www version
RewriteCond %{HTTP_HOST} =www.example.com
RewriteRule (.*) https://example.com/$1 [R=302,L]
# END redirecting www to non www version

It worked for that, so I tried modifying it and using it to also 301 an old post to a new one but it doesn’t work. There are no errors, but it just doesn’t work.

I also tried these codes (found by searching on Google) and almost all of them gave me a 500 Internal error for the whole site.

# BEGIN 301 Redirects
Redirect 301 /example.com/oldpost/
https://example.com/newpost/
# END 301 Redirects

# BEGIN 301 Redirects
rewrite ^/$ https://example.com/oldpost/ permanent;
rewrite ^/(.*)$ https://example.com/newpost/$1 permanent;
# END 301 Redirects

# BEGIN 301 Redirects
server {
    listen 80;
    server_name https://example.com/oldpost/;
    return 301 $scheme:example.com/newpost/$request_uri;
}
# END 301 Redirects

# BEGIN 301 Redirects
server {
    listen 80;
    listen [::]:80;
    hostname https://example.com/oldpost/
https://example.com/oldpost/;
    return 301 https://example.com/newpost/$request_uri;
}
# END 301 Redirects

I know enough to research, find and copy-paste a piece fo code but beyond that I’m at a loss. I would appreciate any help.

Thanks very much in advance.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi @saeeddarabi,

It seems like since you are using Nginx it would be good to have more knowledge about it. Before I write anything else, I’ll recommend you going over the following to DigitalOcean tutorials:

https://www.digitalocean.com/community/tutorials/how-to-create-temporary-and-permanent-redirects-with-nginx

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Both tutorials will provide you the knowledge you’ll need to fully understand how server blocks in Nginx work.

Now, to your problem. You’ll need to add a 301 redirect in your server block for your domain. So let’s assume your domain is example.com. Firstly the conf should be located in the folder /etc/nginx/sites-available/example.com. Open it with your editor, which ever you prefer

nano /etc/nginx/sites-available/example.com

In there, you’ll see something in the midst of

server {
        listen 80;
        listen [::]:80;

        root /var/www/example.comm/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Under the server_name and above the location block, you’ll need to add the following line

rewrite ^/products.html$ /offer.html permanent;

Of course, you’ll need to change the URLs with the first one, in this case -products.html to be your old URL and the second one to be the new one.

Regards,
KDSys

by Justin Ellingwood
When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...

Hi KDSys,

Thanks very much for taking the time to write such a detailed response. Much appreciated.

Yes, you are absolutely right in that I do need to learn more about Nginx. I’m slowly doing it and learning more as I go along.

Thanks for linking to the two guides. Immensely helpful.

I also appreciate your specific solution to my problem. Since I use Server Pilot to manage the server, I can’t access the ETC folder. I have to change some settings I guess, so I am working to do that now. Once that’s done I’ll use the code you provided.

Once again, thanks very much for taking the time out of your busy day to help out.

Submit an Answer