How to get PATH_INFO on Nginx when we use rewrite (friendly url)?

Asked

Viewed 1,078 times

3

I created a .htaccess in the briefcase /var/www/project/:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond $1 !^(statics/([a-zA-Z0-9\-\/.]+)|index\.php)$ #ignora a pasta /statics
    RewriteRule ^([a-zA-Z0-9\-\/.]+)$ index.php/$1 [QSA,L] #Adiciona PATH_INFO
</IfModule>

<Files *.php>
    Order Deny,Allow
    Deny from all
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

index php.:

<?php
echo 'Path: ', $_SERVER['PATH_INFO'];

When access http://localhost/project/profile, index.php returns this:

Path: /profile

The problem is trying to do this on Nginx. I tried this:

location ~ ^/project/(?!index\.php|statics/|data/)([a-zA-Z0-9\-\/.]+)$ {
    rewrite  ^(/project/)([a-zA-Z0-9\-\/.]+)$  $1/index.php/$2 break;
    return 500;
}

location ~ [^/]\.php(/|$) {
    #fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    #if (!-f $document_root$fastcgi_script_name) {
    #    return 404;
    #}

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

But when I open http://localhost:8000/project/profile he shows 404 Not Found.

How can I make Nginx work like . htaccess?

1 answer

1


For it to work it is necessary to use last in the rewrite and fastcgi_split_path_info to configure the PATH_INFO, example:

Note: Use the full path in rewrite and Location

location ~ ^/project/(?!index\.php/.*|index\.php$|statics/.*|data/.*)([a-zA-Z0-9\-\/.]+)$ {
    rewrite ^/project/(?!index\.php/.*|statics/.*|data/.*)([a-zA-Z0-9\-\/.]+)$ /project/index.php/$1 last;
}

location ~ ^/project/(?!index\.php).*\.php$ {
    deny all;
}

location ~ [^/]\.php(/|$) {
    # Configura PATH_INFO
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.