xProteuSx Posted November 26, 2013 Share Posted November 26, 2013 Hi folks I've got a bit of a whopper (I think), especially since I am really unfamiliar and uncomfortable with regex ... I have a website with 46,000+ products. For each product you get a URL that is something like this: http://www.mywebsite.com/viewitem.html?id=12345 Also, because the items are all in a MySQL database, I have PHP generate a <title></title> for each and every product, so for the link above it would be something like this: <title>Silly Shoes: Blue Laces Size 2</title> I would absolutely LOVE to know how to do something like this: MySQL Description -> PHP Title -> custom URL. I am looking to do somethink like what eBay does: http://www.ebay.com/itm/UK-SCOTLAND-10-Pounds-15-11-2007-CLYDESDALE-BANK-PLC-/ Except I would like a url like this: http://www.mywebsite.com/shoes/silly-shoes-blue-laces-size-2/ Is it possible to go from MySQL to PHP to custom URL in one shot? If so, how can I do this? As always, thank you in advance, and CHeers! Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/ Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 Yes it is possible. I have a look at this thread. I helped someone with a similar question to yours. http://forums.phpfreaks.com/topic/283442-how-would-you-query-a-post-in-database-by-title-only/ Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1460160 Share on other sites More sharing options...
xProteuSx Posted November 27, 2013 Author Share Posted November 27, 2013 @Ch0cu3r Thank you for your reply. I have followed the instructions in your post (accurately, I think), but cannot achieve the desired result. Here is what I've got: .htaccess file: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 Here is a sample of the formatting of my links: <a href="viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919">Click Here</a> When I click on the link I simply get: mysite.com/viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919 Ofcourse, the desired result is: mysite.com/armenia-p1-10-rubles-from-1919 (with the "id" being passed to that particular page so that the relevant content is displayed). Where have I gone wrong? Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1460305 Share on other sites More sharing options...
Ch0cu3r Posted November 27, 2013 Share Posted November 27, 2013 The rewrite will only match urls like site.com/<id-here>-<page-title-here> Your urls appear to be the reverse of that format Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1460329 Share on other sites More sharing options...
xProteuSx Posted November 28, 2013 Author Share Posted November 28, 2013 Actually, the way I am trying to do things, and the way I thought I had it set up was a link like this: mysite.com/viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919 Would generate a URL like this: mysite.com/armenia-p1-10-rubles-from-1919 But it would also send the following to that page: $_GET["id"] = 10000; The ID should not show up in the SEO Friendly URL at all ... its in the original URL just so that the page that is being linked to actually has this value. Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1460469 Share on other sites More sharing options...
Ch0cu3r Posted November 28, 2013 Share Posted November 28, 2013 (edited) This rule here RewriteRule ^([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 Will match this url mysite.com/10000-armenia-p1-10-rubles-from-1919 ^ +-- first dash ([0-9]+) matches the numbers before the first dash and passes it as the product id ($1) ([a-z0-9_-]+)/? matches anything after the first dash and passes it as the product title ($2) The rewrite rule will not match this url mysite.com/armenia-p1-10-rubles-from-1919 Edited November 28, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1460500 Share on other sites More sharing options...
xProteuSx Posted November 29, 2013 Author Share Posted November 29, 2013 Yay! I finally got it. Its probably so ridiculous that you guys never considered a human being who is smart enough to use a keyboard to be dumb enough to get this wrong: I was using links like this: mysite.com/viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919 Instead of using links like this: mysite.com/10000-armenia-p1-10-rubles-from-1919 I was under the impression that you had to have the prior to produce the latter. As it turns out, you skip that step and move right onto the latter link (SEO Friendly one). Cheers, and thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1460600 Share on other sites More sharing options...
xProteuSx Posted December 11, 2013 Author Share Posted December 11, 2013 (edited) Hi guys! I am trying to expand on this mod_rewrite idea, and am trying to implement two rules. However, it seems as though only the first is being implemented, even for the second example. Here is what I have in my .htaccess file: ---------------------------------------------------------------------------- RemoveHandler .html .htm AddType application/x-httpd-php .php .htm .html RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9]+)-([a-z]+)/?$ nation.html?id=$1&title=$2 ---------------------------------------------------------------------------- Both link formats are the same. The first re-write rule is used to display specific products within a segment of the gallery. The second re-write rule is supposed to display a sub-segment of the gallery (in this case a country). Both rules utilize a numerical id (each product and each country both have a numerical value associated with them) and a descriptive title. How can I get mod_rewrite to differentiate between 'sub-segment of gallery' and 'specific product'?? At present, if I click on a link that is supposed to send me to a sub-gallery, it will send me to a specific product page if the id numbers for the product and the country are both the same, otherwise I get a MySQL error. ie. If the id number for the sub-gallery is 10, and a product with id number 10 exists then it sends me to product with id 10 page, instead of sub-gallery with id 10 page. ie. If the id number for the sub-gallery is 22, and there is no product with id number 22 then I get a MySQL error (because there is a MySQL query which requires a valid id). Cheers. Edited December 11, 2013 by xProteuSx Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1462054 Share on other sites More sharing options...
xProteuSx Posted December 11, 2013 Author Share Posted December 11, 2013 Oh, I should mention that my links look like this: www.mysite.com/82-paper-money-from-equatorial-african-states Of course, this should display gallery number 82 (paper money from equatorial african states, which is sub-gallery number 82) but instead shows me product 82 on the page). Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1462056 Share on other sites More sharing options...
Ch0cu3r Posted December 11, 2013 Share Posted December 11, 2013 (edited) You need to make the urls for products and and galleries unique, so mod_rewrite can differentiate between the two. I would set up urls to prefix the type of page you are accessing, for example For galleries it would be site.com/gallery/<gallery_id>-<gallery_title>/ For product it would besite.com/product/<product_id>-<product_title>/ As you see having th gallery and product prefixed makes them distinguishable. You'd set the rewrite rules as RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^gallery/([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 [L,NC] # rwrite for gallery RewriteRule ^product([0-9]+)-([a-z]+)/?$ nation.html?id=$1&title=$2 [L,NC] # rewrite for product Edited December 11, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1462058 Share on other sites More sharing options...
xProteuSx Posted February 10, 2014 Author Share Posted February 10, 2014 Man, I sure hope you guys can help me out with this one. I have tried to apply the dual mod-rewrite rules, and only the first works. However, each link is unique, so I don't see the problem. Here is what I've got for the two rules: RewriteRule ^country/([0-9]+)-([a-z]+)/?$ nation.html?id=$1&title=$2 [L,NC] # rewrite for country RewriteRule ^banknote/([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 [L,NC] # rwrite for banknote So the links for the first rewrite rule look like this: banknote/2634-cambodia-p5b-20-riels-from-1956 They work perfectly! The links for the second rewrite rule look like this: country/84-french-afars-and-issas This second link takes me to a 404. Also, I noticed something strange. When one of the 'country' links involves a country that has a single-word name (ie. Poland, Canada, Argentina) the link will take me to a 404, but the address bar will be the referring page. So, for example, if I have the link: country/2-france on page somepage.html, I will get a 404 error on somepage.html. However, if the 'country' link involves a country that has a multi-word name (ie. Papua New Guinea, New Zealand, United States) the link will take me to a 404, but will reflect the initial link. So, for example if I have the link: country/12-new-zealand on page somepage.html, I will get a 404 error on country/12-new-zealand. Anyways, I can't continue working on my project until I get this resolved, as it has almost completely killed the functionality of the site. Please help ASAP!! Its urgent. Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1468362 Share on other sites More sharing options...
Solution xProteuSx Posted February 10, 2014 Author Solution Share Posted February 10, 2014 Found ANOTHER dumb move on my part. nation.html was not accepting a variable named 'id'; it was looking for a variable named 'country'. I simply changed the rewrite rule to this: RewriteRule ^country/([0-9]+)-([a-z_-]+)/?$ nation.html?country=$1&title=$2 [L,NC] Now everything is peachy! Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/284295-php-generated-title-custom-url/#findComment-1468365 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.