Jump to content

url redirect with a id code in url


richrock

Recommended Posts

I'm in the process of updating a website, but we also have to modify htaccess to do 301 redirects on the existing links, for reasons not strictly needed.

 

90% of this has gone well, but:

 

I have thousands of dynamic SEF urls similar to this -

 

www.example.com/product/2234/producttitle.html

 

Which would need to be redirected, to something like:

 

www.example.com/catalogue/1009/producttitle.html

 

- I would guess I need to send this to a script to process, but how do I refer to a php script to run this query through?  The 2234 is the product ID in the database, and 1009 is the Catalogue ID (shows how many need to be done  :o )

 

Any pointers greatly appreciated.  .htaccess is not my strong point, in fact only really learnt stuff today!

 

EDIT = OR......

 

change to a non-sef url like this:

 

index.php?option=com_items&id=2234&lang=en&task=viewitem

 

where I can put the 2234 from the url at the top into the url above....

Link to comment
Share on other sites

There is no way that .htaccess can be used solely to achieve this. There is no direct correlation between the two URLs since one includes the number '2234' and the other includes the number '1009'. I guess you could re-direct to a script that can knows the relationship between these two numbers to change one to the other and perform another redirect via header.

 

.htaccess

RewriteEngine on
RewriteRule ^product/([0-9]+)/[a-zA-Z-]+\.html$ /redirect.php?id=$1 [R=301]

redirect.php

$product_id = intval($_GET['id']);
$sql = "SELECT catalogue_id, title FROM table WHERE product_id = $product_id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$catalogue_id = $row['catalogue_id'];
$title = $row['title'];
header('Location /catalogue/{$catalogue_id}/{$title}.html");

Obviously the example makes a lot of assumptions.

 

Link to comment
Share on other sites

excellent!  I can work it from there, I think :D

 

One other query - I have a number of fixed pages using ?page=something on the url - I've tried doing simple redirects, but these are still adding the appended ?page=something on the url, instead of going to site.com/something.html??

 

:confused:

Link to comment
Share on other sites

Well, it's a case of just redirecting to the nearest usable page, as the whole site has been rebuilt, etc.

 

So in the old site it would have had:

 

product.html?prod_id=3

 

We're not going to bother with linking to the extact product again (there are 30,000 items, a lot of headaches could ensue in testing) so we'll go to the nearest main catalogue page, eg:

 

product/catalogue.html

 

Without the appended query string...

 

Hope this makes sense...  ::)

Link to comment
Share on other sites

Assuming the exact product ID is in the old URL, I see no reason not to forward directly to the new URL, you don't have to test 30,000 links just test 1 or 2, if that works then they will all work as a single pattern should apply to them all. With mod_rewrite there are 3 key pieces of information...

1.) The actual location on the server of the file you wish to be displayed.

2.) The URL that you wish to appear in the address bar.

3.) The URL that is used in the link on your page.

 

Generally speaking if you are starting from scratch 2 & 3 should be the same, they will normally be 'pretty URLs' which are then used as Aliases to the ugly URL. For example I may have a file in my DocumentRoot folder called products.php. I decide I want all products to be accessed using http://domain.com/product/10. What I would do is make all URLs on my site look like that. Then I would have these files...

 

.htaccess

RewriteEngine On
RewriteRule ^product/([0-9]+)/?$ /products.php?id=$1

 

products.php

$id = intval($_GET['id']);
$sql = "SELECT * FROM products WHERE id=$id";

Now if you are attempting to redirect an old site, the situation may be different. In order to provide any meaningful help we would need to have examples of those 3 pieces of information.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.