This nginx rule works great for me for a full specified file path

rewrite ^/sitemap.xml$ /sitemap.php last;

When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.

Another problem arised and I need to rewrite the last part of existing urls

rewrite ^doctor-solution.html/ doctor-answer.html/ permanent; What I want to achive is when an old url like https://example.com/case12232-doctor-solution.html/ is accessed it must be redirected to


But My rule doesn't seem to work. Any ideas?

Can I do something like, this gives me error

 rewrite ^/([a-z]+)-doctor-solution.html/$ /{$1}-doctor-answer.html/;
  • Try: rewrite ^/([a-z]+)-doctor-solution.html/$ /$1-doctor-answer.html/ permanent; – Richard Smith Jan 27 at 10:08

You forget to include digit in your regex , and made a error to reference a matching pattern , so this is $1 and not {$1} ( see Richard Simith comment )

In you case the solution

rewrite ^/([a-z0-9]+)-doctor-solution.html/$ /$1-doctor-answer.html/ redirect;

More documentation on https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

  • unfortunately I get error 404 – bornie Jan 27 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.