Noz
Noz

Reputation: 6346

NGINX handle all php requests in a non-php web application

Our company website used to run on PHP but has now been moved over to Ruby On Rails and no longer contains any .php file extensions in the url. We wan't to try and handle all the legacy url requests still containing the .php extension and simply redirect them to the requested page without the extension. Is this possible in NGINX?

Previously I was doing something to the effect of:

location /somesubdirectory/somepage.php {
  return 301 http://example.com/somesubdirectory/somepage;
}

However, for reasons beyond me the above code only seems to handle the redirect sometimes. After an unknown period of time it just up and breaks, and then at random works like magic again - likely a separate configuration issue on my end. Additionally, even if the above worked as anticipated, it would quickly become cumbersome to define one of these for the 50+ pages on my website so I'm really looking for a more elegant solution than this.

Upvotes: 0

Views: 114

Answers (1)

Alexey Ten
Alexey Ten

Reputation: 14354

This location block should redirect all php requests.

location ~ \.php$ {
    rewrite ^(.*)\.php$ http://example.com$1 permanent;
}

Upvotes: 1

Related Questions