How do I redirect from /file.php to /file on nginx?

by june.crooks , in category: SEO , 7 months ago

How do I redirect from /file.php to /file on nginx?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by elmo.conroy , 7 months ago

@june.crooks 

You can redirect from /file.php to /file on nginx using the following configuration in your nginx server block:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;
    server_name example.com;

    # Redirect /file.php to /file
    location = /file.php {
        return 301 /file;
    }

    # Other configurations
    ...
}


Explanation:

  • The location = /file.php block matches requests for /file.php exactly.
  • The return 301 /file; statement returns a 301 redirect response with the Location header set to /file.


This configuration will redirect all requests for /file.php to /file with a permanent redirect (301).