0

I'm using Jelastic with nginx 1.6.1 and would like to use friendly urls without the .php extension. So if someone visits for example https://domain.com/faq.php nginx should rewrite it to https://domain.com/faq and displaying the content.

I spent a few hours to get this working by trying solutions like:

location / {
    try_files $uri $uri.php $uri/;
}

without success and I always get an 404 error if I try to access for example domain.com/faq

Does anybody have a working solution? Here's my default nginx.conf file:

worker_processes  1;
error_log /var/log/nginx/error.log;

events {
    worker_connections 1024;
}


http {

    server_tokens off;
    include mime.types;
    default_type application/octet-stream;

    log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log  main;
    sendfile on;
    keepalive_timeout 65;

    server {

        listen 80;
        server_name localhost;
        include /etc/nginx/aliases.conf;
        index index.php index.html index.htm;
        error_page 500 502 503 504  /50x.html;

        location / {
            root /var/www/webroot/ROOT;
            index index.html index.htm index.php;

            location ~ \.php$ {
                location ~ /\. { deny all; access_log off; log_not_found off; }
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME /var/www/webroot/ROOT$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT /var/www/webroot/ROOT;
            }

        }

        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            location ~ /\. { deny all; access_log off; log_not_found off; }
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT /var/www/webroot;
        }

        include /etc/nginx/conf.d/*.conf;

    }
}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50

1 Answers1

0

I get that you want to rewrite faq to faq.php and if that is the case, all you need to do is:

rewrite ^/faq$ faq.php last;

And that's it.

Nemke
  • 248
  • 5
  • 10