richrock Posted April 20, 2010 Share Posted April 20, 2010 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 ) 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.... Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/ Share on other sites More sharing options...
cags Posted April 20, 2010 Share Posted April 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045221 Share on other sites More sharing options...
richrock Posted April 21, 2010 Author Share Posted April 21, 2010 excellent! I can work it from there, I think 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?? Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045664 Share on other sites More sharing options...
cags Posted April 21, 2010 Share Posted April 21, 2010 RewriteEngine On RewriteRule ^([a-zA-Z0-9-]+)\.html$ /index.php?page=$1 Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045682 Share on other sites More sharing options...
richrock Posted April 21, 2010 Author Share Posted April 21, 2010 Oops, I may have mis-worded my question - I need to remove the ?page=something, redirecting to somthing.html instead? Sorry if I'm a little dull, I'm only 24hrs into htaccess (other than redirecting domains ) Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045684 Share on other sites More sharing options...
cags Posted April 21, 2010 Share Posted April 21, 2010 What URL do you wish to have in the address bar? Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045685 Share on other sites More sharing options...
richrock Posted April 21, 2010 Author Share Posted April 21, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045696 Share on other sites More sharing options...
cags Posted April 21, 2010 Share Posted April 21, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199146-url-redirect-with-a-id-code-in-url/#findComment-1045711 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.