How can I do a 301 simple URL redirect?

March 29, 2016 760 views
Linux Basics Linux Commands

Hi community!

I need to redirect some URL to root, because we redesign all entire site to new urls and now its going to 404 page, and I dont know how but Im having problems with that.

url : /contacto.htm to domain.

I include this command in server block

server {
location = /contacto.htm {
return 301

Its good or bad? I dont know... I hope that you can help me. I need to do 12 redirects...

Kind regards!

1 Answer


Yes, that works. However, if you will be doing 12 different redirects, it might be easier to use a regex pattern instead:

location ~* ^(contacto\.htm|file2\.htm|etc) {
    return 301;

Make sure you escape the periods so that RegEx does not parse them as the wildcard character (which matches anything) by adding a backslash before each one. So contacto.htm becomes contacto\.htm.

  • Thank you much for your reply!

    But it

    location = /contacto.htm {
    return 301

    doesnt work so well because of .htm, I dont know how but not working... :|

    • Can you post an example redirect that you want to accomplish? If you browse to http://yourdomain/contacto.htm, does it not redirect to

      Make sure you restart nginx every time you edit a config file so that the changes take effect:

      sudo service nginx restart
Have another answer? Share your knowledge.