justlukeyou Posted July 6, 2013 Share Posted July 6, 2013 Hi, I have added a link name to database called $linkname. For example "large-red-widget". However my link is querying the database using a unique product ID but I want to display the $linkname within link. What I dont understand is the relationship between the htaccess file and the way the link is displayed. How I do I display link with linkname but query the databsae using the product_id ? This works... /products/product/<?php echo $row['product_id']; ?>" > RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?product_id=$1 [code] This doesn't... /products/product/<?php echo $row['product_id']; ?>" > [code] RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?linkname=$1 [code] Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/ Share on other sites More sharing options...
justlukeyou Posted July 6, 2013 Author Share Posted July 6, 2013 Hi, I just found this really neat peice of code which appears to tidy up the page much better and doesn't require me to rewrite each and every page. However I am still unsure how to query with one variable but display another variable in the link. <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439693 Share on other sites More sharing options...
justlukeyou Posted July 6, 2013 Author Share Posted July 6, 2013 Hi, I having Googling and as far as I can tell I need add a 'dummy slug'. However I cant see how I add a dummy slug onto the query. Do I need something like this? /products/product/<?php echo $row['product_id']; ?>/<?php echo $row['linkname']; ?>/ Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439702 Share on other sites More sharing options...
requinix Posted July 6, 2013 Share Posted July 6, 2013 This works... /products/product/<?php echo $row['product_id']; ?>" > RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?product_id=$1 Then... use it? This doesn't.../products/product/<?php echo $row['product_id']; ?>" > RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?linkname=$1 Then... don't use it? I just found this really neat peice of code which appears to tidy up the page much better and doesn't require me to rewrite each and every page. <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule> I don't see how that can help you since it'll send everything (that doesn't exist) to index.php when you want to send those specific URLs to /products/product.php. Hi, I having Googling and as far as I can tell I need add a 'dummy slug'. However I cant see how I add a dummy slug onto the query. Do I need something like this? /products/product/<?php echo $row['product_id']; ?>/<?php echo $row['linkname']; ?>/ Maybe. 1. What URLs currently work? The non-rewritten, unfriendly ones. /products/product.php?product_id=123? 2. Look at the pieces of that URL that can vary with the products. Looks like just the product_id. 3. What URL do you want to use? The rewritten, friendly ones. /products/product/123/Name? 4. Does that second URL contain the varying pieces of the first URL? It needs to. It can have other things too, like the product name, but at a minimum it needs to include the product_id somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439708 Share on other sites More sharing options...
justlukeyou Posted July 6, 2013 Author Share Posted July 6, 2013 This code seems to work the same as having to write out each individual page so it makes the code a lot simpler. <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule> What I am stuck on is using a variable as a slug within the link. This is a classic example of what I am looking to achieve. johnlewis.com/john-lewis-lasko-chest-of-drawers-oak/p230674902 Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439709 Share on other sites More sharing options...
requinix Posted July 6, 2013 Share Posted July 6, 2013 1. What URLs currently work? The non-rewritten, unfriendly ones. /products/product.php?product_id=123? 2. Look at the pieces of that URL that can vary with the products. Looks like just the product_id. 3. What URL do you want to use? The rewritten, friendly ones. /products/product/123/Name? 4. Does that second URL contain the varying pieces of the first URL? It needs to. It can have other things too, like the product name, but at a minimum it needs to include the product_id somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439710 Share on other sites More sharing options...
justlukeyou Posted July 6, 2013 Author Share Posted July 6, 2013 1. This currently works... /products/product/713972992 2. Not sure what this means. But product_id is the only unique reference I have. 3. Looking for something like this... /products/product/713972992/red-widget or /products/product/red-widget/713972992 4. Not sure what this means. Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439711 Share on other sites More sharing options...
requinix Posted July 6, 2013 Share Posted July 6, 2013 (edited) You've got a $ on the end of whatever you're using now, right? What are you using? Remove that and replace it with, like, ($|/). Then you can use whatever URL you want so long as it starts with /products/product/713972992/. That fits the first pattern you're considering; say something if you'd rather the second. [edit] On the subject, make sure your product.php figures out what the right URL is supposed to be, compares it to what was actually used, and redirects if the two don't match. That's for SEO: you shouldn't have multiple URLs that work for the same page. Edited July 6, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439713 Share on other sites More sharing options...
AbraCadaver Posted July 6, 2013 Share Posted July 6, 2013 Just an example, not tested, but if you go with this one: /products/product/red-widget/713972992 Then your htaccess could be: <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [L] </IfModule> And then in index.php or an include at the top, something like: // if you need all parts to make more decisions // $parts = explode('/', $_GET['url']); // $product_id = last($parts); // for just the last $product_id = basename($_GET['url']); // SELECT blah FROM blah WHERE product_id = $product_id This will have to be expanded upon because you need to know it's products and not faq or whatever else, but all the info is in the $_GET['url'] for you to parse and use etc.. so you can use the explode() method. Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439714 Share on other sites More sharing options...
AbraCadaver Posted July 6, 2013 Share Posted July 6, 2013 Oh, and if it were me, I'd probably lose the product and just have: /products/the_most_awesome_widget/713972993 and then you can always use the first part to decide what controller or page to load and the last part for the unique id. It's your app so you decide what is where and it makes it simpler. switch dirname($_GET['url']) { case "products": $product_id = basename($_GET['url']); // or whatever you use in the queries, could always be $id include('products.php'); break; // etc... default: die('bad request'); } Or get creative and do it another way that's simpler or more scalable for you or your project. A lot of frameworks use this same approach. I normally use CakePHP for larger DB projects like this. Saves days, weeks or months. .There's always whether you are displaying a single product or listing all products or all products in a category etc, so think about all those and map them out. In that case you may want to do something like /products/view/the_most_awesome_widget/713972993, so that for others you can do products/category/red_widgets/7 to view all products in category 7 (red widgets) Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439717 Share on other sites More sharing options...
justlukeyou Posted July 6, 2013 Author Share Posted July 6, 2013 Aw hell, now the links to the pages don't work. Well some work but others dont. Unbelievable. This links works. .com/articles/articles But this doesn't .com/products/products I've tried reverting back to my old files but they are still not working. Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L] # Disable directory listing from this point Options -Indexes # Prevent viewing of htaccess file <Files ~ "^\.ht"> order allow,deny deny from all satisfy all </Files> # Error Pages ErrorDocument 404 /404-error.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ /products/product.php?product_id=$1 </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439728 Share on other sites More sharing options...
justlukeyou Posted July 7, 2013 Author Share Posted July 7, 2013 // if you need all parts to make more decisions // $parts = explode('/', $_GET['url']); // $product_id = last($parts); // for just the last $product_id = basename($_GET['url']); // SELECT blah FROM blah WHERE product_id = $product_idThis will have to be expanded upon because you need to know it's products and not faq or whatever else, but all the info is in the $_GET['url'] for you to parse and use etc.. so you can use the explode() method. Many thanks, Im not sure what this part means though. What part of the code allows two variables to be in a link but only the second variable is the query. Quote Link to comment https://forums.phpfreaks.com/topic/279917-pretty-urlshtaccess-file/#findComment-1439753 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.