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_pass 127.0.0.1:9000;
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
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, yourrewrite
is backwards. You want the URLhttp://www.example.com/foo/
to runfoo.php
, so the rewrite would need to beBut 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
http://www.example.com/foo//
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:You could also do something like
So that URLs could be
example.com/questions/1
example.com/questions/1/
example.com/questions/1/how-do-I-rewrite
butexample.com/questions/banana
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.