0

I am rebuilding an Old Website, all pages ends with .php extension.

Eg: example.com/contactUS.php
    example.com/static-article.php

I need to change it to

 Eg: example.com/contact-us
     example.com/article

I am not just removing .php, changing full URL to more relevant one. so old URL Must be redirected using 301.

so our 404.php should receive all details including all request header , Referer etc.

then 404.php will check if the URL actually existed or not, from an array of deleted pages. If requested URL is present in the array, 404.php will pass the request to new page with 301 status. if the requested URL not present in my array, it will return custom 404 error.

How to do this using Nginx?

Vishnu
  • 1
  • 1

1 Answers1

0

error_page 404.php tells nginx to send requests to this script if corresponding file is not found.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • 404.php should know which URL was requested. Eg: if user requested example.com/static-article.php , now Nginx should feed 404.php?URL=example.com/static-article.php , now 404.php can look at the deleted array of URLs and issue a 301 to example.com/article. I need to redirect over 2500 pages. If the requested URL not found on the deleted pages array, 404.php will return 404 status. – Vishnu Jul 30 '22 at 12:52
  • The requested URI is available in `$_SERVER['REQUEST_URI']` variable. There is no need to pass it via GET parameter to the script. – Tero Kilkanen Jul 30 '22 at 19:45
  • $_SERVER['REQUEST_URI'] from 404.php getting requested URL. That is good, but already returned 404 STATUS Code. For SEO purpose we need to redirect user to new page, if page was deleted without retuning 404 but 301. – Vishnu Jul 30 '22 at 21:31
  • You need to set the headers properly in `404.php`: https://www.systutorials.com/permanent-redirect-with-http-301-in-php/ – Tero Kilkanen Jul 31 '22 at 08:14
  • error_page 404 404.php; getting 301 and $_SERVER['REQUEST_URI'] changes to 404.php (due to redirect, Multiple 301 is bad) error_page 404 /404.php; PHP redirect is not working. – Vishnu Jul 31 '22 at 10:21