Nginx config rewrite with PATH_INFO

倾然丶 夕夏残阳落幕 提交于 2021-02-08 10:13:54

问题


I create .htaccess file in folder /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)$ #ignore folder statics
    RewriteRule ^([a-zA-Z0-9\-\/.]+)$ index.php/$1 [QSA,L] #Add 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 I open a url like this http://localhost/project/profile, my index.php show this:

Path: /profile

The problem is that I am unable to do this in 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 if open http://localhost:8000/project/profile show 404 Not Foud.

How can I Nginx function identically to .htaccess?


回答1:


Use last in rewrite and fastcgi_split_path_info for fix the PATH_INFO, eg.:

Note: use entire path in rewrite (like example) and in 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(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;#Fix PATH_INFO

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



回答2:


Try this: http://winginx.com/en/htaccess

# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^/([a-zA-Z0-9\-\/.]+)$ /index.php/$1 break;
}
}
location ~ *.php {
deny all;
}
location /index.php {
allow all;
}


来源:https://stackoverflow.com/questions/30019954/nginx-config-rewrite-with-path-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!