Search Marketing


Using PHP to Redirect a URL

Tue Oct 27, 2009 1:42 pm
Comments: 0 Views: 286

You can use Apache to redirect a url but if you need to use a 301 redirect in a more complex way then PHP makes it very easy to do a redirect also. This is true if you are moving to a different url scheme such as id numbers to plain text. If you start out with one type of url system then you need to make sure you can still be found through the old url system.

Apache is a powerful way to move or redirect your urls to an older format but it may not be possible if you need to access a database to decide which page you are going to display.

Lets assume you have something like:

and you want to have a more uniform and informative url scheme such as

You first need to determine what needs to be in the url for your program to understand how deliver the proper information. Does the date need to be in the url? Does the category information?

If you wrote your program so that there were unique database identifiers such as absolutely unique product numbers then you can simply read the old url and then look for the product number and redirect the visitor to the correct url.

Lets also assume you have constructed the database so that you can find which category any given product belongs to. This may not be the case if you have floating categories or do not use categories, in which case you are not concerned with that information.

function readurl()
// global $_GET; or $productid = $_GET['product'];
// read the url and find the product variable
// find the product information in the database using the variable
// reconstruct the url and use a 301 redirect to make sure you tell search engines that the old url has moved permanently
// info from database

$categoryname = from database;
$productname = from database;
$newurl = "$categoryname/$productname";
$x = Header("HTTP/1.1 301 Moved Permanently");
$x .= Header("Location: $newurl");

Before you construct the program to read the old url you must write a program to fix all urls in your database. The main thing is that you want to have a fixed url, but with the ability to be modified in the editing state. The other place you want read out your new urls is how you display them on the page, such as in your navigation or sitemaps.

One of the easiest ways to do this is to add an extra table field onto your product information in the database called url. Then write a program to loop through all your product titles and create a web friendly url. Or you could do this by hand in the editing process for each product.

URL: http: (ex.
Math (25 + 4)
* required

© 2019 Christonium LLC
Terms of Use