SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 51
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Pretty Urls (htaccess file?)

    Hi,

    I am querying my database with a unique product code. However I want the URL to dispaly the $linkname does anyone know how I do this please.

    For example: /products/product/<?php echo $row['product_id']; ?>" >

    Currently:

    /products/product/123456

    Change to:

    /products/product/red-widget

  2. #2
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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


    This doesn't...

    /products/product/<?php echo $row['product_id']; ?>" >


    RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?linkname=$1

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    This works...

    /products/product/<?php echo $row['product_id']; ?>" >

    RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?product_id=$1


    This doesn't...

    /products/product/<?php echo $row['product_id']; ?>" >


    RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?linkname=$1
    Firstly you'd have to change the code in product.php to query your db on the linkname column, rather than the product_id. You'd also need to change the code that generates your links, so that they include the name rather than the id, similar to this:
    PHP Code:
    /products/product/<?php echo $row['linkname']; ?>
    Are you using some kind of ecommerce software or anything like that?

  4. #4
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No its a site a built myself using PHP.

    The thing is I want to run the query using a unique code which I am currently doing. How do I query the database using one variable but display a different variable in the link.

    This is a classic example of what I am trying to do.


    johnlewis.com/john-lewis-lasko-chest-of-drawers-oak/p230674902

    But is it possible to do this without the product ID. So its just but still displays product "p230674902"

    johnlewis.com/john-lewis-lasko-chest-of-drawers-oak

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    What table structure do you have? Is all the product info in one table, and then you have a second table that maps the linkname to the product_id?

  6. #6
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No these two variables are in the same table.

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    So instead of the existing query, which must be something along these lines:
    Code:
    SELECT * FROM products WHERE product_id = $product_id
    why can't you just do this?
    Code:
    SELECT * FROM products WHERE linkname = $linkname

  8. #8
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Because the link name is not a unique identifier.

    I thought I would need a unique identifier?

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Because the link name is not a unique identifier.

    I thought I would need a unique identifier?
    Right, yes you would.. sorry, I'd assumed that your link name was unique. In the case that it's not, what you have to do is include the product ID in the url, something like johnlewis.com/p230674902-john-lewis-lasko-chest-of-drawers-oak to use your example. I expect this brings you back to square one, but unfortunately if the link name is not unique then there is no way around it.

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,832
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Because the link name is not a unique identifier.

    I thought I would need a unique identifier?
    Your "link name" is commonly referred to as a slug. You need a unique identifier somewhere in your URL to retrieve the appropriate page. If you have redundant slugs, you will have to have a product ID or some other unique identifier in your URL.

    yoursite.com/products/123456/large-red-widget

    123456 is the product ID in this case which would be used to query the table. The large-red-widget is a dummy slug in this case for the benefit of people or search engines reading the URL and is not used in the code. So you could do something like this:

    yoursite.com/products/123457/large-red-widget

    Same dummy slug but different product ID.

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Its very strange because when I try to run the query with the linkname and change the GET on the following page the following page does not display anything.


    Code:
    /products/product/<?php echo $row['linkname']; ?>
    
    <?php
    if (isset($_GET['linkname']))
    $linkname = mysql_real_escape_string($_GET['linkname']);
    $sql = "SELECT * FROM productdbase WHERE linkname = '$linkname'";
    $res = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
    $num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an ID in the url without clicking on your link
    ?>

  12. #12
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    Your "link name" is commonly referred to as a slug. You need a unique identifier somewhere in your URL to retrieve the appropriate page. If you have redundant slugs, you will have to have a product ID or some other unique identifier in your URL.

    yoursite.com/products/123456/large-red-widget

    123456 is the product ID in this case which would be used to query the table. The large-red-widget is a dummy slug in this case for the benefit of people or search engines reading the URL and is not used in the code. So you could do something like this:

    yoursite.com/products/123457/large-red-widget

    Same dummy slug but different product ID.

    Thanks,

    This looks good. How do I add the 'dummy slug' into the link whilst still querying the unique ID.

  13. #13
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,832
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Thanks,

    This looks good. How do I add the 'dummy slug' into the link whilst still querying the unique ID.
    PHP Code:
    /products/<?php echo $row['product_id'] . '/' $row['linkname']; ?>
    Something like that. I didn't test. Also, do you have a bug in your if syntax from earlier where you forgot the brackets?

  14. #14
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    You also need to change your rewrite rule a little:
    Code:
    RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ /products/product.php?product_id=$1
    This should let you have URLs in the same format as in cheesedude's example.

  15. #15
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried that but it failed. Should I have htaccess file like this and link as per below?


    Code:
    <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>
    
    /products/product/<?php echo $row['product_id']; ?>/<?php echo $row['linkname']; ?>" >

  16. #16
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried this but it doesn't display anything.


    Code:
    <?php echo $row['product_id'] . '/' . $row['linkname']; ?>

  17. #17
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Hi,

    I tried that but it failed. Should I have htaccess file like this and link as per below?


    Code:
    <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>
    
    /products/product/<?php echo $row['product_id']; ?>/<?php echo $row['linkname']; ?>" >
    I think you might need to swap the order of those last two rules, and add a couple of flags like this:
    Code:
    RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ /products/product.php?product_id=$1 [QSA, L]
    RewriteRule ^ index.php [L]
    but I'm not an expert in mod_rewrite rules, so you might want to run it past someone over in this forum:
    http://www.sitepoint.com/forums/foru...-URL-Rewriting

  18. #18
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Ive tried Googling "slugs" and "pretty urls" but nothing is coming up.

    Is there a best basic guide on how to use slugs in pretty urls?

  19. #19
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Hi,

    I tried this but it doesn't display anything.


    Code:
    <?php echo $row['product_id'] . '/' . $row['linkname']; ?>
    Try calling var_dump($row) to check if you're actually getting a record back from the DB.

  20. #20
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried that and it does echo. I can echo it into the link and treat it as a normal variable.

    Im stuck on using it as a slug within the link.

  21. #21
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Hi,

    I tried that and it does echo. I can echo it into the link and treat it as a normal variable.

    Im stuck on using it as a slug within the link.
    Let me just check that I understand what you're saying.. basically you can generate a link OK, which looks something like this /products/12345/my-product-name, but if you navigate to that URL in your browser, the product ID is not being passed to your script via $_GET['product_id'], is that the problem?

  22. #22
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes that it is right.

    I cant add the linkname to the link without it interfering with the query.

  23. #23
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    I managed to recreate the problem on my local server, and I managed to solve it by making a couple small changes to the .htaccess file.

    First, add this line to the top of the file:
    Code:
    Options -MultiViews
    Then browse to the test URL, and you'll probably get a 404 Not Found error. If you do, you'll also have to change this line, removing the / in front of products:
    Code:
    RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ products/product.php?product_id=$1 [QSA,L]

  24. #24
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I tried but couldn't get it to work.

    This is the code I am using.

    Code:
    <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 [QSA,L]
    </IfModule>
    Should I be producing the link like this?

    Code:
    <?php echo $row['linkname'] . '/' . $row['product_id']; ?>

  25. #25
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    You need to switch your last two rewrite rules around, like this:
    Code:
    RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ products/product.php?product_id=$1 [QSA,L]
    RewriteRule ^ index.php [L]
    your RewriteRule ^ index.php [L] rule is basically a catchall, and any rules you put after it will never be reached.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •