Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I want to rewrite a url like to

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;

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;



share|improve this question
up vote 4 down vote accepted
try_files $uri $uri/ $uri.php /index.html;
share|improve this answer
Ohhh I get it, cool. Totally approached that wrong I guess. Cheers. – Doug Sep 21 '12 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 '12 at 1:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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