Darshan Montgomery Darshan Montgomery - 5 days ago 4x
PHP Question

Nginx rewrite not working with .php extension

I'm trying to do a url rewrite via this code:

location ~ /canada1/[A-Za-z0-9-_]+/.* {
rewrite ^/canada1/([A-Za-z0-9-_]+)/.* /atemplates/weather/weather_forecast.php?region=$1&location=$arg_db last;

With this url:


I've narrowed down the issue to the .php portion. Whatever I stick in there to replace it works fine as expected i.e. .phd .phq .abcdefg ... works fine. What do I need to do to make that thing rewrite with that .php extension?

Thanks for your help.

Reference: How to rewrite old url with a period, question mark and equals in it?


Regular expression location blocks are evaluated in order.

If you want location ~ /canada1/[A-Za-z0-9-_]+/.* to take precedence over location ~ \.php$, then you need to place the first one before the second one, in your configuration file.

For example:

location ~ /canada1/[A-Za-z0-9-_]+/.* {
location ~ \.php$ {

See this document for details.