我在这个目录树中build立了一个PHP项目:
“项目”是networking服务器的根源。
我想跳过url的“公开”部分,但保留“v1”:
http://myserver.com/v1/whatever/1
并不是
http://myserver.com/v1/public/whatever/1
我得到了这个工作:
rewrite ^(.*)$ /v1/public/index.php?_url=/$1;
另外我希望能够通过这个URL结构来处理“documentation”和“examples”中的文件 :
http://myserver.com/v1/documentation/file_name.php http://myserver.com/v1/examples/file_name.php
我使用这两个.htaccess文件在Apache中工作:
#/项目/ V1 /的.htaccess 重写引擎 RewriteRule ^ $ public / [L] RewriteRule(。*)public / $ 1 [L]
#/项目/ V1 /公/的.htaccess
RewriteEngine On
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。*)$ index.php?_url = / $ 1 [QSA,L]
我尝试将这些设置转换为Nginx失败。 这是我的结果:
server { listen 80; server_name mysite.com; index index.php index.html index.htm; set $root_path '/var/www/html/project'; root $root_path; try_files $uri $uri/ @rewrite; location @rewrite { rewrite ^(.*)$ /v1/public/index.php?_url=/$1 break; } location ~ \.php { fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index /index.php; include /etc/nginx/fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { root $root_path; } location ~ /\.ht { deny all; } }
首先,你不需要location ~* ^/(css|img|js|flv|swf|download)/(.+)$ block,因为它不会改变任何东西。 另外,一般情况下, location块内的root目录不是你通常想要的, alias是你最想要的东西。
但是,对于实际的问题,请尝试添加这些位置块:
location /v1/documentation/ { rewrite ^/v1/documentation/(.+)$ /v1/public/documentation/$1 last; } location /v1/examples/ { rewrite ^/v1/examples/(.+)$ /v1/public/examples/$1 last; }
针对您的@rewrite块的可选优化
location @rewrite { rewrite ^ /v1/public/index.php?_url=$uri break; }