【问题标题】:Nginx config rewrite with PATH_INFO使用 PATH_INFO 重写 Nginx 配置
【发布时间】:2015-05-04 23:16:27
【问题描述】:

我在文件夹/var/www/project/ 中创建.htaccess 文件:

<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'];

当我打开像这样http://localhost/project/profile 的网址时,我的 index.php 会显示:

Path: /profile

问题是我无法在 Nginx 中执行此操作。我试过这个:

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;
}

但如果打开http://localhost:8000/project/profile 显示404 Not Foud

如何让 Nginx 功能与 .htaccess 相同?

【问题讨论】:

    标签: php apache .htaccess nginx rewrite


    【解决方案1】:

    rewritefastcgi_split_path_info 中使用last 来修复PATH_INFO,例如:

    注意:rewrite(如示例)和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】:

      试试这个: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;
      }
      

      【讨论】:

      • 我试过了,但是因为没有文件夹的准确性,所以没有用。看我的回答。但感谢您的尝试,谢谢。
      猜你喜欢
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2015-04-03
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多