将用户从page.php重定向到使用nginx的页面

I have the following server block:

server {
    listen 80;
    server_name petpal.co.il;
    root /usr/share/nginx/petpal;
    index index.php;
    rewrite ^/([a-zA-Z]+)\-([0-9\-]+)$ /$1.php?page=$2? last;
    rewrite ^/en/([a-zA-Z]+)\-([0-9\-]+)$ /en/$1.php?page=$2? last;
    location / {
        try_files $uri $uri/ @extensionless-php;
    }
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }
    set $lang "";
    if ($uri ~ "^/en/") {set $lang "en/";}
    error_page 404 /${lang}notfound;
    location ~ \.php$ {
        try_files $uri =404;
        client_max_body_size 4M;
        fastcgi_read_timeout 300;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

It already handles url rewriting (process petpal.co.il/dogs as dogs.php), however, I want to avoid people from reaching page.php so there won't be duplicated pages (same page named x and x.php), so all I want to do is adding a rule that will redirect people from page.php to page without .php OR at least showing a 404 not found page (what do you think is better?)

Hope I explained it well, any clues? Thanks!

I'm not sure if I fully understand your question but instead of writing a rule you could do the following.

  • Create a dogs folder
  • Create an index.php file inside it and edit that file

Now if you go to petpal.co.il/dogs it will show petpal.co.il/dogs/index.php.

Users can still go to dogs/index.php but it isn't a duplicate page.

Hope it helps!