Following is what I have right now and it works but needs some improvement. I am guessing that I will need to use regex and that is why I need help.

What I need is this

http://www.example.com/filename/segment1/segment2/?argk=argv

to go to this:

http://www.example.com/filename.php/segment1/segment2/?argk=argv

Following works without the segments. the index.php works as expected

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

location ~ \.php$ {

    try_files $uri $uri/ /index.php?$args;

    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #   fastcgi_intercept_errors on;
    fastcgi_pass  unix:/tmp/php.socket;
}

Stuff I tried:

if (!-e $request_filename)
{
    rewrite ^/([^?]*[^?/])/?(\?.*)?$  /$1.php/$2  last;
    break;
}

    if (!-e $request_filename)
    {
        rewrite ^/([^./]+)/(.*) /$1.php/$2 last;
        break;
    }

You'll need the rewrite directive from the rewrite module along with a regular expression to perform an internal rewrite of the path. Something like this inside the location / block should work.

rewrite ^/([^./]+)/(.*) /$1.php/$2 last;

You'll also need to modify the regular expression of your PHP location block to match .php files in the middle of the path, so something like this:

location ~ \.php(/|$)
  • Tried this but I get a 404 page if (!-e $request_filename) { rewrite ^/([^./]+)/(.*) /$1.php/$2 last; break; } – RoboTamer Jul 24 '12 at 5:55
location ~ ^(/[^/]+)(.*)$ {
    try_files $uri $1.php$2$is_args$args;
}

location ~ ^(.+\.php)(/.*)?$ {

    try_files $uri /index.php?$args;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$1;
    fastcgi_param SCRIPT_NAME $1;
    fastcgi_param PATH_INFO $2;

    #   fastcgi_intercept_errors on;
    fastcgi_pass  unix:/tmp/php.socket;
}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.