Im trying to rewrite a url like below:
https://example.com/products/xperia-z5/ to--> https://example.com/xperia-z5/
But i want in the same time the url https://example.com/products/
to be accessible, without any modifications since its a product catalog.
For organizing reasons i keep my files in /products/file1, file2 etc. Perhaps i should be using "alias" and not "rewrite"?
Perhaps i must change something in the try_files directive, or something is wrong with the @extensionless-php location, i'm totally confused. Please advice.
Thank you.
Below is my server.conf config
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;
}
}
When i try to request http://192.168.10.1/xperia-z5/ in the logs im getting this (with 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"
After a lot of trial and error, the sulution turned out to be:
I hope this will help someone in the future.