0

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:

  1. example.com redirects to www.example.com
  2. www.example.com/index redirects to www.example.com/
  3. www.example.com/page.php redirects to www.example.com/page (remove .php)
  4. www.example.com/resources should working if I have same folder and file name
  5. www.example.com/sdfgdsgdfsgjdf shows 404.html because file doesn't exist
  6. Params in url are working e.g ?lang=en&test=test
  7. 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; 
2
  • Try: try_files $uri $uri.html $uri/ @extensionless-php; in the location / block. Commented Nov 18, 2024 at 8:48
  • @RichardSmith tried and no. 4 is not working, including 404 page doesn't appear Commented Nov 18, 2024 at 19:50

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.