I have the following code, tried so many attempts, 3 days working on it but still nothing. If I fix one thing, another breaks - looking for help.
What I'm trying to achieve:
example.comredirects towww.example.comwww.example.com/indexredirects towww.example.com/www.example.com/page.phpredirects towww.example.com/page(remove .php)www.example.com/resourcesshould working if I have same folder and file namewww.example.com/sdfgdsgdfsgjdfshows404.htmlbecause file doesn't exist- Params in url are working e.g
?lang=en&test=test www.example.com//must have double slash disabled
Currently this code works except point 5. Every not existing page is showing index page.
I almost fixed all above by replacing try_files $uri /index.php$is_args$args; with try_files $fastcgi_script_name =404; but then page www.example.com is not working, only www.example.com/index works
server { listen 80; server_name example.com; return 301 https://www.example.com/$request_uri; } server { listen 80; server_name www.example.com; root /var/www/html; index index.html index.php; error_page 404 /404.html; error_page 500 502 503 504 /404.html; location / { try_files $uri $uri.html @extensionless-php; } location ~ \.php$ { try_files $uri /index.php$is_args$args; fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_send_timeout 180s; fastcgi_read_timeout 180s; } location @extensionless-php { rewrite ^(.*)$ $1.php last; } location = /404.html { internal; } } server { listen 80 default_server; server_name _; root /var/www/html/health-check; location / { index index.php index.html index.htm; try_files $uri $uri.html =404; } } So far I tried this many attempts to fix it in different order:
# rewrite ^/$ /index; # rewrite ^/(.*)/$ /$1 permanent; # rewrite ^(.+)/+$ $1 permanent; # rewrite ^(.+)/index.html$ $1 permanent; # rewrite ^/$ /index; # Too many redirects error # location = /index.php { # return 301 /; # } # try_files $uri /index.php; # try_files $uri =404; # try_files $uri /index.php?$args; # try_files $uri /index.php?$args =404; # try_files $uri $uri/ @extensionless-php; # try_files $uri $uri.html @extensionless-php; # try_files $uri $uri/ /index.php?$args @extensionless-php; # Breaks folders and same file names # try_files $uri $uri/ /index.php$is_args$args; # Shows 404 on index page when ? in url # try_files $uri /index.php$is_args$args =404; # Everything works except index # try_files $fastcgi_script_name =404; # Breaks url # fastcgi_param PATH_INFO $fastcgi_path_info;
try_files $uri $uri.html $uri/ @extensionless-php;in thelocation /block.