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

Basically, I want the ".php" to be replaced with a "/". I tried to just put my rule in the same location where the fastcgi is located.

location ~ \.php$ {
                fastcgi_index index.php;
                include fastcgi_params;
                rewrite ^(.+)\.php$ /$1;

However, when I restart nginx with these settings, it gives me an 500 Internal Server Error and I can't even get the server to show me anything. Is this because of fast cgi? How can I work around this?

My system: nginx / php5-fpm / apc / php5-memcache / Ubuntu 10.10

share|improve this question
It's not clear what you're trying to achieve. If you rewrite the URL from foo.php to /foo (you say you want .php replaced with "/" but your rewrite has the slash on the wrong side of $1), then PHP will try to execute /foo which probably does not exist. Moving the slash to the end means PHP will try to execute foo/ which still won't work. – DerfK Aug 23 '11 at 15:29
Thanks for your info DerfK. I want foo.php to become foo/. Are you saying this is not even possible? – Frank Vilea Aug 23 '11 at 15:36
What do you mean "become"? You want people to click on a link that says "foo/" and foo.php is executed? – DerfK Aug 23 '11 at 16:02
Yes, and I don't want to have any .php in the address bar because I want to create a clean URL such as the one here in serverfault without any endings. – Frank Vilea Aug 23 '11 at 16:06
up vote 2 down vote accepted

Essentially, your general plan has a lot of problems with it. First, since you are removing the .php, the rewrite has to be outside of location ~ .php$ since the URL won't end in .php until after it is rewritten. Second, your rewrite is backwards. You want the URL to run foo.php, so the rewrite would need to be

rewrite ^(.+)/ $1.php;

But this means that EVERY directory on your website would need a matching .php file since you will no longer be able to use index.php automatically. Also, if nginx does not de-duplicate slashes before rewriting, if someone mistypes an address as then it will execute /foo/.php.

Finally, how many PHP files are you planning on creating? Do you really think serverfault has a 304162.php file for this question? (not that serverfaults' URL would match your rewrite since it doesn't end in /, nor do they use PHP I think)

Normally, clean URLs work by declaring a specific location (for instance /questions/) then rewriting everything AFTER that URL to be run by a single script:

rewrite ^/questions/(.*)$ /questions.php?path=$1 last;

You could also do something like

rewrite ^/questions/([0-9]+)(/.*)?$ /questions.php?qid=$1&extra=$2 last;

So that URLs could be but would be rejected for not having a numeric question id.

Take a look at how people set up clean URLs for Drupal or Wordpress. Essentially, every request for a file that does not exist is rewritten as /index.php?q=$1, where index.php looks at the "q" variable to figure out what it is supposed to do.

share|improve this answer
Just short FYI, you don't actually need the q parameter for Wordpress, you can just rewrite to /index.php and Wordpress will do just fine. – Martin Fjordvald Aug 23 '11 at 17:25

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.