How can this PHP be written so that the URL is clean?

Discussion in 'PHP' started by uleesgold, Feb 6, 2012.

  1. #1
    <?php
    generateMenu();
    $default = 'home'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.
    $page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page.
    if (!file_exists('inc/'.$page.'.php')) { //Checks if the file doesn't exist
    $page = $default; //If it doesn't, it'll revert back to the default page
    //NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.
    }

    include('inc/'.$page.'.php'); //And now it's on your page!
    ?>


    this is the output of the URL:

    sitename.com/article/?p=articlename


    I don't want the ugly '?p=' to be there.

    I've searched for 'clean url php' and it seems like when I've used something to try and trim that, its over ridden by the PHP used to generate the menu.

    Do you think .htaccess could trim this off? I know its common to use .htaccess for prettier URL's but I wouldn't want this PHP to get in the way.
     
    uleesgold, Feb 6, 2012 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    yes.. .htaccess is your answer there, use RewriteRule
     
    bartolay13, Feb 6, 2012 IP
  3. NothingLikeThis

    NothingLikeThis Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    definitely .htaccess helps you in cleaning all the php urls to neat urls
     
    NothingLikeThis, Feb 6, 2012 IP