Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

After a lot of googling, I've managed to get pretty URLs working by using:

server {
    listen   80;
    rewrite ^/(.*)$1 permanent;

server {
    listen   80;

    access_log /var/www/;
    error_log /var/www/;

    location / {

    root   /var/www/;
    index  index.html index.htm index.php;
    include /etc/nginx/mime.types;

    try_files $uri $uri/ @extensionless-php;


    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;

    location /blog {
        root /var/www/;
        index index.php;
        try_files $uri $uri/ /blog/index.php?$args;


It seems to work. However, for SEO purposes, I'd like to permanently redirect requests from to the new

I gather that I should be using rewrite ^/(.*)\.php$$1 permanent; in the .php location block, but how do I do it without causing an endless loop?

share|improve this question
You should post at least the entire server block, so that it's possible to determine that nothing else is interfering with your desired behavior. – Michael Hampton Feb 19 '13 at 4:38
Thanks for the tip - I've edited it to include the original full server block. – nonchalant Feb 19 '13 at 4:52

Your Answer


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

Browse other questions tagged or ask your own question.