Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
    }
share|improve this question

2 Answers 2

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(/|$)
share|improve this answer
    
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;
}
share|improve this answer

Your Answer

 
discard

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.