Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How to add page title in url in a dynamic site

    I have a website called gapagap.com that is developed using php.

    What I want to know is how can I change my url from this pattern:

    http://www.gapagap.com/gag/54

    to this:

    http://www.gapagap.com/gag/then-why-...illionaire-yet

    As you can see that basically I want the page title to appear at the end of url and not the post sequence number.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Is the 54 a numeric id associated with the title? If so, you don't need to change a lot.
    This is currently using a mod_rewrite to convert the url into one that is appropriate for the script in question. It's highly probable that you do not have site.com/gag/xx/index.[php|html|whatever] where xx is an id, and more likely that you have a site.com/gag.php where xx is then provided as querystring gag.php?id=54 for example. Assuming that's the case, and also assuming that id is always numeric, and also assuming that the rewrite rules allow it through, you can use the current rules and modify the underlying code.
    Somewhere you will have something along the lines of:
    PHP Code:
    if (isset($_GET['id']))
    {
        
    $id $_GET['id'];
        
    // query where ID = $id (minding SQL security as well)

    So say you have a query of SELECT id, title, joke FROM gag WHERE id = $id. Now all you need to do is detect if it's numeric, and if it is, use the id, otherwise use the title.
    PHP Code:
    if (isset($_GET['id']))
    {
        if (
    is_numeric($_GET['id']))
        {
            
    $sQry "SELECT id, title, joke FROM gag WHERE id = " . (int)$_GET['id'];
        }
        else
        {
            
    $title escape($_GET['id']);
            
    $sQry "SELECT id, title, joke FROM gag WHERE title = '$title'";
        }

    Where escape a function to clean up the input. Now you can query the $sQry and get based on either.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

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