301 redirect nginx

May 12, 2014 52.2k views
Hello! I need 301 redirect from site.com/category/something to site.com/something/
1 comment
5 Answers
You can use a rewrite rule, like so:


rewrite ^/category/something/(.*)$ http://site.com/something/$1 redirect;


And remember to restart nginx:


service nginx restart


Check out this article:

https://www.digitalocean.com/community/articles/how-to-create-temporary-and-permanent-redirects-with-apache-and-nginx
by Justin Ellingwood
HTTP redirects are used to redirect web site visitors to new locations when they request content. These are useful for a variety of reasons, whether permanent or temporary. This guide will demonstrate how to redirect visitors using Apache and Nginx.
Thanks for answer but i have 404

Hi admin
I want change old a URL https://www.bandatnendongnai.vn/du-an/
New URL https://www.bandatnendongnai.vn/category/du-an/
I using nginx. Thank you so much

  • location /du-an {
    rewrite ^/du-an(.*) https://$server_name/category/du-an/$1 permanent;
    }

    service nginx restart

server {
listen 80;
server_name www.bandatnendongnai.vn bandatnendongnai.vn 202.182.98.49;
rewrite ^(.*) https://www.bandatnendongnai.vn$1 permanent;
rewrite ^/du-an(.*) https://$server_name/category/du-an/$1 permanent;
}
server {
    listen       443 ssl http2;
    server_name  bandatnendongnai.vn;
    return       301 https://www.bandatnendongnai.vn$request_uri;
    rewrite ^/du-an(.*) https://$server_name/category/du-an/$1 permanent;
ssl_certificate /etc/nginx/paidssl/www.bandatnendongnai.vn/www.bandatnendongnai.vn.crt;
ssl_certificate_key /etc/nginx/paidssl/www.bandatnendongnai.vn/www.bandatnendongnai.vn.key;

and then service nginx restart

Check nginx
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Please help me. Thank you so much

Have another answer? Share your knowledge.