Home > Blockchain >  Redirect a php link to a page in wordpress
Redirect a php link to a page in wordpress

Time:04-23

I had a e-commerce website using php and now have to switch on wordpress but the problem is redirecting previous php links to new pages on wordpress :-

example :- https://domain-name/file.php?id=321 is the previous link now I have to redirect it on https://domain-name/product/product-name/

pov: file.php does'nt exist on the server it's just a previous link

CodePudding user response:

Think you are looking for 301 redirects aka permanent redirect, which points both Google bots and site visitors to the new page.

You can use the free "Redirection" Plugin to achieve this .

CodePudding user response:

I would create a file.php in your document root to handle these URLs.

You can create php files on a WordPress webserver and you don't need to do anything special to get them called. If the file exists, WordPress will just allow it to handle its own requests.

Using PHP is the correct choice for this job for a few reasons:

  • It is already powering WordPress, so you don't need to do anything special to get PHP running on the webserver
  • The URL already has a .php extension, so no rewrite rules or URL mappings will be required
  • It will require you to look up the product name from the id. Hard coding a big list of lookups in .htaccess can slow every request to your website. Having a big list in a PHP redirect handler will only get loaded for requests that trigger a redirect.
<?php

// Map ids to product names
// This could also be done with a database query 
// rather than a hard coded associative array
$redirects = array(
  "321"=>"/product/product-name/", 
  "456"=>"/product/blue-widget"
);

// If there is an id on the URL and a mapping for it exists
if (isset($_GET["id"]) and isset($redirects[$_GET["id"]])){
  // Send a permanent redirect to WordPress
  header("HTTP/1.1 301 Moved Permanently"); 
  header("Location: " . $redirects[$_GET["id"]]); 
  exit();
}

// Otherwise fall back to showing a 404 page.
http_response_code(404);

?>
  • Related