Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a problem. I had an old dynamic url which I have now split into multiple static urls.

e.g.

www.mydomain.com/product.php?type=1&id=2
www.mydomain.com/product.php?type=2&id=3
www.mydomain.com/product.php?type=2&id=4, etc

which I have changed to something like

www.mydomain.com/electronics/radio
www.mydomain.com/electronics/television
www.mydomain.com/mobile/smartphone, etc.

Google has previously indexed the dynamic urls and search results show the old urls. I want search to point to the new urls.

I have kept the old url active, so both urls work.

How can I set up a 301 redirect in this case? I run IIS and it only allows a page to be redirected to 1 url.

Should I deactivate the old dynamic url? In that case I lose all the previous seo rankings..

share|improve this question
add comment

3 Answers

you could leave product.php as it is (displaying product base on type and id) but add categoryname and productname GET variables (and code to map them to an actual product), so you can redirect the new type of url mysite.com/(category)/(product) to product.php?categoryname=$1&productname=$2 I don't know ISS so I cant suggest the actual code for the rule

EDIT: I think I misunderstood your question at first. The way I do it is adding something like this in PHP (i see you are using PHP, but you could do similarly in any language):

if (strpos($_SERVER['REQUEST_URI'],'/product.php') === 0)
{
  // redirect to www.mydomain.com/category/product/
}

PS: Sorry if I've answered instead of commented, but I cant comment yet

share|improve this answer
    
So I would need to rewrite product.php to contain a series of if/else statements like this: –  user16136 Jun 10 '12 at 5:05
1  
No, only one. It means if the typed/linked URL its actually showing product.php, you do some stuff (query the db, see what category is, and redirect there possibly), otherwise you carry on normally (means you are coming from /category/product already –  milo5b Jun 10 '12 at 12:35
    
Now I am confused, Milo. I thought I would put a series of if-else statements in product.php and redirect to the appropriate /category/product page. But you said only once. That makes sense too as I can query the database to find which category and product to redirect to. But I do not understand in which php file I would have the code you suggest. I was thinking of putting this in product.php, but you said "otherwise you carry on normally", I thought I am already in product.php and only need to find out what is the category and product. There is no else condition... –  user16136 Jun 10 '12 at 18:41
    
That is, there is already a index.php in each sub-folder to handle the case where the entry is directly to /category/product page.. The problem arises only when someone comes thru product.php?type=3&id=4, etc. So the entry is in product.php and therefore it is in this page that I should have the code you suggest. In which case there is no need to search for "product.php" in the request url. Am I right or totally off-track? Sorry if I am being plain dumb. Thanks for ur responses so far. –  user16136 Jun 10 '12 at 18:49
    
Oh sorry, I thought you still had product.php doing that job, but responding to a rule redirecting /electronics/ipod to product.php?category=electronics&item=ipod. In this case, if you have already a index each directory, you should redirect from product.php (after you query for right /category/product/ url) to /category/product/ page. Dont even need that if condition, just query the DB, get cat and item name, and redirect there. Hope I could explain myself, if not please let me know I am very bad at explaining! –  milo5b Jun 10 '12 at 21:38
add comment

It could be a bit of work, but you shouldn't have old dynamics url created anymore.

Then you can redirect each old dynamic url (their number is limited).

share|improve this answer
    
Thanks samX, I have stopped creating any new dynamic urls. It wasnt until I paid attention to SEO that I realized that dynamic urls are not the best option. –  user16136 Jun 10 '12 at 6:05
add comment

It's not clear to me if this url www.mydomain.com/electronics/radio is just an alias for www.mydomain.com/product.php?type=1&id=2 created by the same PHP script, or if they are two separate PHP scipts (even if they display out the same contents), the old script is product.php and the new PHP script is index.php

According to some of your comments

That is, there is already a index.php in each sub-folder to handle the case where the entry is directly to /category/product page..

I would say we are in the 2nd case, so you have two separate PHP scripts.

What you need to do is to simply redirect the old pages i.e. www.mydomain.com/product.php?type=1&id=2 to the new ones i.e. www.mydomain.com/electronics/radio via a 301 permanent redirect.

Therefor you simply need to add this code at the top of product.php script:

<?php

/*
1. Look in your DB/code to find out:
- what category name corresponds to type=1 (in the example "electornics")
- and what item name corresponds to id=2 (in the example "radio")
I suppose you create a couple of functions getCategoryNameFromType/getItemNameFromId
in order to do this tedious job.
The functions return empty string when they don't find a correspondence
*/
$category_name = getCategoryNameFromType( $_GET['type'] );
$item_name = getItemNameFromId( $_GET['id'] );

//if both not empty strings
if( $category_name && $item_name )
{
   //2. Creating webpage url to redirect
   $webpage = "http://www.mydomain.com/" . $category_name . "/" . $item_name . "/";

   //3. Redirecting 301 permanent
   header("Location: " . $webpage, TRUE, 301);
   exit();
}

?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.