5

I want url domain.com/foo-111 to load contents from directory /bar/111 but not change url.

I created a rewrite rule but instead of loading contents of proper directory, it 301 redirects to domain.com/bar/111

My server config

server {
  listen 80;
  server_name domain.com;

  location / {
      root /var/www/domain.com;
      index index.html index.htm;
      rewrite ^/foo-(.*)$ /bar/$1 break;
      try_files $uri $uri/ =404;
  }
}
Viacheslav
  • 187
  • 1
  • 3
  • 8

1 Answers1

7

nginx is trying to add a trailing / to turn the URI into the correct format for a directory spec. Add the / in your rewrite so that nginx doesn't have to. Try this:

 rewrite ^/foo-(.*?)/?$ /bar/$1/ break;
Richard Smith
  • 12,834
  • 2
  • 21
  • 29