Question

Need help with page redirect on Nginx

Posted January 25, 2020 167 views
Nginx

Hi all,

I’m struggling with a redirect issue. I need to redirect a specific page to another due to naming changes.

www.example.com/blogs to www.example.com/blogs/all

Just need to set a permanent redirect for the above example. I’ve set this in default .conf file in many different ways but nothing works.

Tried rewrite, return, permanent ways in server block but didn’t help.

There is already a server block in .conf file that was added for http to https redirect. Not sure if there should be only one server block or something.

Created a redirects.conf file as well and tried but nothing works. Any help will be greatly appreciated!

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.

×
1 answer

Hi @saipradyz,

You need to set the rewrite of your page in your server block in the .conf file of the domain.

This is how a permanent redirect from a page to another one looks like

server {
# Permanent redirect to an individual page
rewrite ^/blogs http://www.example.com/blogs/all permanent;
}

Please note you need to have a separate configuration file for your domain -

/etc/nginx/sites-available/domain.com.conf
/etc/nginx/sites-enabled/domain.com.conf

Regards,
KDSys

  • Hello,

    Many thanks for your response.

    I did create 2 new conf files as suggested under sites available and enabled

    and added the server block in those new files like below:

    server {
    # Permanent redirect to an individual page
    rewrite ^/blogs http://www.example.com/blogs/all permanent;
    }
    

    and then did a nginx service reload on the server. Tested but no change. Page doesn’t redirect still :(

    Anything else I’m missing here? Also fyi there is already a default conf file under sites-available. Tried adding this piece of code in that file too in the existing server block but didn’t help

    Is there any other way to tell the server the entire existing URL and the new URL it has to redirect to. I suspect if it’s unable to pick up the ^/blogs thing. Can we add the full URL here? As I need direct 1-1 URL mapping.. Many thanks

    edited by MattIPv4
Submit an Answer