0

I have this old website URL structure:

site.com/folder/prod.php?cat=MAIN%20CAT%20&prodid1=123&prodtitle=PROD%20TITLE&subcat=SUB%20CAT

and real example will be something like:

site.com/folder/prod.php?cat=CAR%20AUDIO&prodid1=4444&prodtitle=MTX%20AMPS&subcat=AMPS

here you can see that for the product page there are 4 variables: category, produt id, product title and sub category. Some of this variables were used to open a menu. And yes, the URL pulls variables with space and both lower and uppercase.

The new site url has a new structure:

site.com/x/product-title-prodid2

a rel example will be like:

site.com/x/mtx-amps-8888

Which is accomplish by using two variables (friendly slug + a second product id: prodid2) with the following code in the .htaccess

<IfModule mod_rewrite.c>
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^p/(.*)/$ product.php?prodid2=$1
RewriteRule ^p/(.*)$ product.php?prodid2=$1

</IfModule>

Internally we can get prodid2 if we have prodid1 from the same table, but not viceversa.

Everything works fine, but we now have to create 301 redirects and apparently since the same variables are not used in the old / new url, then it becomes tricky since apparently we have to create a single rule for the nice URL creation and the 301s?

We have tried adding the following to the htaccess:

RewriteCond %{QUERY_STRING} ^cat=CAR%20AUDIO&prodid1=4444&prodtitle=MTX%20AMPS&subcat=AMPS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]

and it works for only 1 product, but when adding 2 or more, the site goes down. I imaging this would be an infinite loop?

An alternative would be adding a:

ErrorDocument 404 /404.php

to get the URL and redirect to the page, but this would be ugly for SEs.


UPDATE:

Sorry for my lack of understanding on this topic, am very new to this.

The product has 2 important ids. For example:

MTX AMP (which is the actual product title) if listed in 3 categories will have 1 single prodid2 repeated and 3 different prodid1 (1 for each category). They all reside in the same table. So, if we have a prodid1 we can get the prodid2 which is right next to it in the db table.

The rule to get a nice URL on the new site is pulled using prodid2

RewriteRule ^p/(.*)$ product.php?prodid2=$1

which brings the complete value stored in the database. e.g. mtx-amps-8888 << this is a mix of a slug + the prodid2

complete url is:

site.com/p/mtx-amps-888

(the p is just a virtual forder and we take advantage of that variable to show the right page template)

So mtx-amps-888 are not 3 keys, these are generated when creating a product and saved all together in a single field in the db. They already include the separation - so this is not done in the htaccess.

The cat (key) value is really used to expand a menu used in the old site with, but to create the 301 redirect we would probably use prodid1 since we can match that value to get a prodid2. prodid2 is used as the main query to get the nice URL in the new site and its value will bring the nice URL stored in the db.

What makes sense from all my research would be the following:

<IfModule mod_rewrite.c>
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^p/(.*)$ product.php?prodid2=$1

RewriteCond %{QUERY_STRING} ^cat=CAR%20AUDIO&prodid1=4444&prodtitle=MTX%20AMPS&subcat=AMPS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]

RewriteCond %{QUERY_STRING} ^cat=CAR%20AUDIO&prodid1=5555&prodtitle=BOSS%20AMPS&subcat=AMPS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]

RewriteCond %{QUERY_STRING} ^cat=CAR%20VIDEO&prodid1=6666&prodtitle=ALPINE%20DVDS&subcat=DVD%20PLAYERS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]

</IfModule>

Pls note that I removed a line from the main rewrite rule:

RewriteRule ^p/(.*)/$ product.php?prodid2=$1

This only assures that the user can also use / at the end of the URL: site.com/p/mtx-amps-888/

I also repeated the rewrite condition for the 301 redirects of 3 products, but i really have about 3K products to list here. If I keep 1, it will work but if I add 2, I believe a loop is created.

Hopefully this makes sense. You have no idea how important is for me to get this up and running, so my best wishes to those who can help :)

  • 1
    Really confusing. ¿What is and how do you get `prodid1` mentioned in "...Internally we can get prodid2 if we have prodid1..."? Are there always 3 keys (product-title-prodid2)? ¿Are they always separated by `-`? In a query, `cat`, for example, is a **key** and `CAR%20AUDIO` is a **value**. It will be more clear to use those attributes in your examples, like key1=value1, etc. – Felipe Alameda A Dec 23 '12 at 00:24
  • Thanks for the quick response, am amazed of how quick people are ready to assist. Hope that I can share my expertise soon as well. – user1924270 Dec 23 '12 at 01:59

1 Answers1

0

Just re-create the file /folder/prod.php and have php do the redirect. This is the easiest and cleanest solution.

<?php
  $prodid1 = $_GET['prodid1'];
  //calculate prodid2 based on prodid1, or use mysql to retreive the prodid2 belonging to prodid1
  $prodid2 = $prodid1;//just for testing
  $newpath = "/p/$prodid2/";

  // redirect using 301
  header("Location: http://{$_SERVER['HTTP_HOST']}{$newpath}");
  header('HTTP/1.1 301 Moved Permanently');
?>
Gerben
  • 16,747
  • 6
  • 37
  • 56