I'm a little new to Nginx here so bear with me -

I want to rewrite a url like foo.bar.com/newfoo?limit=30 to foo.bar.com/newfoo.php?limit=30.

Seems pretty simple to do it something like this rewrite ^([a-z]+)(.*)$ $1.php$2 last;

The part that I am confused about is where to put it - I've tried my hand at a some location directives but I'm doing it wrong.

Here's my existing virtual host config, where should I implement my rewrite?

server {
listen   80;
listen   [::]:80;

server_name foo.bar.com;
root /home/foo;

index index.php index.html index.htm;

location / {
    try_files $uri $uri/ /index.html;

location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    deny all;

location ~ \.php$ {
    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;



1 Answer 1

try_files $uri $uri/ $uri.php /index.html;
  • Ohhh I get it, cool. Totally approached that wrong I guess. Cheers.
    – Doug
    Sep 21, 2012 at 1:41
  • Just a note, I got this to work by putting it at the top of the .php file location directive, and renaming that directive to location / {}. Thanks again
    – Doug
    Sep 21, 2012 at 1:50

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.