Darshan Montgomery Darshan Montgomery - 1 month ago 9
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:

/canada1/bc_british_columbia/weather.php?db=90


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?

Answer

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.