使用 PATH_INFO 重写 Nginx 配置
Nginx config rewrite with PATH_INFO
我在文件夹 /var/www/project/
中创建 .htaccess
文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond !^(statics/([a-zA-Z0-9\-\/.]+)|index\.php)$ #ignore folder statics
RewriteRule ^([a-zA-Z0-9\-\/.]+)$ index.php/ [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'];
当我打开 url 这样的 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\-\/.]+)$ /index.php/ 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 相同?
在rewrite
和fastcgi_split_path_info
中使用last
来修复PATH_INFO
,例如:
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/ 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;
}
试试这个:http://winginx.com/en/htaccess
# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^/([a-zA-Z0-9\-\/.]+)$ /index.php/ break;
}
}
location ~ *.php {
deny all;
}
location /index.php {
allow all;
}
我在文件夹 /var/www/project/
中创建 .htaccess
文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond !^(statics/([a-zA-Z0-9\-\/.]+)|index\.php)$ #ignore folder statics
RewriteRule ^([a-zA-Z0-9\-\/.]+)$ index.php/ [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'];
当我打开 url 这样的 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\-\/.]+)$ /index.php/ 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 相同?
在rewrite
和fastcgi_split_path_info
中使用last
来修复PATH_INFO
,例如:
Note: use entire path in
rewrite
(like example) and inlocation
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/ 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;
}
试试这个:http://winginx.com/en/htaccess
# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^/([a-zA-Z0-9\-\/.]+)$ /index.php/ break;
}
}
location ~ *.php {
deny all;
}
location /index.php {
allow all;
}