Removing index.php from url

adambenayoun nginx-forum at nginx.us
Mon Dec 3 00:08:55 UTC 2012


Hey all,
I have a server with nginx 1.2.5 and php-fpm 5.3.3 installed. I have a web
application built on top of zend framework and the routing passes everything
thru a index.php file.
So while http://www.domain.com/contact works -
http://www.domain.com/index.php/contact will fetch the same content aswell
and I'd like to get rid of it for SEO purpose.

Here's my configuration - right now there's a if statement that takes care
of removing the index.php however I know that if is evil and therefore
shouldn't be used. On top of that the only instance that won't work well is
http://www.domain.com/index.php which in this case instead of redirecting to
www.domain.com will just display a blank page with a 301 status code.

On top of that - if you guys spot any problem with my config - I'd love to
hear what I am doing wrong and what can be improved.


server {
listen *:80;
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
server_name www.domain.com;
ssl_certificate /etc/nginx/certs/www.domain.com.crt;
ssl_certificate_key /etc/nginx/certs/www.domain.com.key;
error_log /var/www/domain/logs/error_log warn;
root /var/www/domain/html/http;
index index.php;
client_max_body_size 150m;
error_page 403 404 http://www.domain.com/notfound;

if ( $request_uri ~ "^/index.php" ) {
rewrite ^/index.php(.*) $1 permanent;
}
location / {
rewrite ^/wanted/feed$ /feed?filter=wanted permanent;
try_files $uri $uri/ /index.php?$args;
}
location /min {
try_files $uri $uri/ /min/index.php?q=;
}
location /blog {
try_files $uri $uri/ /blog/index.php?q=$1;
}
location /apc {
try_files $uri $uri/ /apc.php$args;
}
location ~ \.php {
include /etc/nginx/fastcgi_params;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SERVER_NAME $http_host;
fastcgi_pass 127.0.0.1:9000;
}
location ~* ^.+\.(ht|svn|git)$ {
deny all;
}

# Static files location
location ~*
^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
expires max;
}
}

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,233476,233476#msg-233476



More information about the nginx mailing list