我正在尝试重写如下 URL:
https://example.com/products/xperia-z5/ to--> https://example.com/xperia-z5/
但我希望同时https://example.com/products/
可以访问 URL,无需进行任何修改,因为它是一个产品目录。
出于组织原因,我将文件保存在 /products/file1、file2 等中。也许我应该使用“别名”而不是“重写”?
也许我必须改变一些东西try_files指令,或者出现问题@extensionless-php位置,我完全搞不清楚。请指点。
谢谢。
以下是我的 server.conf 配置
server {
server_name 192.168.10.1;
listen 80;
root /home/webmaster/example.com/html_public;
charset UTF-8;
# replace .php extension with trailing slash
location @extensionless-php {
rewrite ^(.*)/$ $1.php last;
rewrite ^(.*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ @extensionless-php;
}
error_page 404 /404.php;
#pass the PHP scripts to FastCGI server listening on php-fpm unix socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
location /products/$ {
rewrite ^/products/(.*)$ /$1 break;
}
}
当我尝试请求时http://192.168.10.1/xperia-z5/在日志中我得到了这个(404):
2016/04/25 16:50:19 [notice] 10191#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/25 16:50:19 [notice] 10191#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/25 16:50:19 [notice] 10191#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/25 16:50:19 [notice] 10191#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/25 16:50:19 [info] 10191#0: *1 recv() failed (104: Connection reset by peer), client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
答案1
经过多次反复尝试,最终的解决方案是:
location / {
try_files $uri $uri/ @extensionless-php;
rewrite /(.+$) /products/$1 break;
}
location = /products/ {
index index.php;
}
我希望这能对将来的某人有所帮助。