2

I want do a redirect from old url:

http://example.org/xxxxxxxxx.html

To new urls (remove ".html")

http://example.org/xxxxxxxxx

How I can do this with nginx?

EDIT:

xxxxxxxxx can be differ, example:

http://example.org/url-1.html redirect to http://example.org/url-1 http://example.org/another-url.html redirect to http://example.org/another-url

cnst
  • 25,870
  • 6
  • 90
  • 122
rpayanm
  • 6,303
  • 7
  • 26
  • 39
  • op, did my answer satisfy your question? if yes, please upvote and accept! if no, feel free to provide clarifications of where it falls short, if it does. – cnst Jul 06 '16 at 03:26
  • thanks for accept&&upvote, +1 your way! – cnst Jul 11 '16 at 16:33

3 Answers3

4
location ~ ^(.*)\.html$ {
    return 301 $1;
}
cnst
  • 25,870
  • 6
  • 90
  • 122
1

Probably you need a rewrite statement

location /xxx.html {
   rewrite ^/xxx(.*) http://example.org/xxxxx permanent;
 }

You detailed explanation please refer https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Another method would be return directive

server {
    listen 80;
    listen 443 ssl;
    server_name www.old-name.com old-name.com;
    return 301 $scheme://www.new-name.com;
}
Amarpreet Singh
  • 2,242
  • 16
  • 27
0
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.org www.example.org;
    return 301 http://$server_name$request_uri;
}
nPcomp
  • 8,637
  • 2
  • 54
  • 49