Mahesh
Mahesh

Reputation: 1267

redirect all product_details.php?id=p_id to mysite/products/product_name

i have configured my Wordpress website product detail's permalink to follow "Mysite/products/product_name" url pattern but the old pattern "mysite.com/product_details.php?id=1" still working.

is there any way i can redirect the old url to new one .

Upvotes: 1

Views: 411

Answers (5)

Aminul Islam
Aminul Islam

Reputation: 73

You will need to edit the product_details.php file and add the code as below.

<?php
if(!empty($_GET['id']) {
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://YOUR_DOMAIN.TLD/?p=" . $_GET['id']); 
    exit();
}
?>

Please replace YOUR_DOMAIN.TLD with your domain.

The above code will redirect to your main site with id and it will generate the correct link and redirect.

Upvotes: 0

Torge Rosendahl
Torge Rosendahl

Reputation: 564

Put something like the followng in your product_details.php:

<!DOCTYPE html>
<html>
   <head>
      <title>HTML Meta Tag</title>
      <meta http-equiv = "refresh" content = <?php
            echo "\"1; url = products/"
            echo $_GET['id']
            echo "\""
      ?> />
   </head>
   <body>
      <p>redirecting...</p>
   </body>
</html>

source for redirection part

You might want to replace the $_GET['id'] part with snippet that translates the product id to the according product name and echo that into the link

Upvotes: 0

Magnus Ingwersen
Magnus Ingwersen

Reputation: 411

This is not very well supported in Wordpress itself...

I would suggest, as the others here are saying; to apply manual redirects using a plugin. Have a look at how that approach is done safely in this article: https://www.wpexplorer.com/change-permalinks-wordpress/

Upvotes: 2

Kaumadie Kariyawasam
Kaumadie Kariyawasam

Reputation: 1456

Setting Up Manual Redirects

Sometimes, the plugin won’t pick up on the redirect you need, maybe because you want to redirect one post to another or you want to redirect a URL on your domain to a different domain.

In this case, you’ll need to set up a manual redirect.

Go to Tools > Redirection and scroll down to the Add new redirection section. add new redirect

In the Source URL field, type or paste in the URL you want to redirect from. In the Target URL field, type or paste in the URL you want to redirect to. In the Group field, either leave it as Redirections (the default) or select Modified posts if you’re directing from an old post to a newer version. This will give browsers information telling them what kind of redirect it is.

Finally, click the Add Redirect button and your manual redirect will be added to the list of redirects.

Upvotes: 0

Andrew Hulterstrom
Andrew Hulterstrom

Reputation: 1725

The easiest way would probably be to use a plugin such as https://wordpress.org/plugins/redirection/.

Upvotes: 0

Related Questions