Grow your CSS skills. Land your dream job.

PHP Switch/Case and the Dreaded Unclean URLs

  • # March 20, 2013 at 2:18 pm

    Actually, I went ahead and changed that, but for some reason it'[s not working like it should. When I go to just http://rpotterjr.dev it displays the 404 page instead of the home page… Wth? I never had this problem with that…

    # March 20, 2013 at 2:26 pm

    Weird, can you show me your full index.php file?

    # March 20, 2013 at 2:44 pm

    The index.php file contains nothing but an include to the header, content, and footer…

    # March 20, 2013 at 2:45 pm

    where the content is simply the case/switch that handles the page display…

    # March 20, 2013 at 2:46 pm

    Without the mod_rewrite it displays the 404 page only when going to a wrong link, but with it, the 404 becomes the default…

    # March 20, 2013 at 2:49 pm

    You aren’t giving me much to work with.

    Try debugging yourself by placing echos and var_dumps on crucial places (where things might go wrong) so you know where and why things go wrong. I recommend `var_dump($page)` right before your switch since that seems to be the problem.

    # March 20, 2013 at 3:59 pm

    I think I may just go ahead and handle the error page through .htaccess

    # March 20, 2013 at 7:46 pm

    > Thanks goes out to BenWalker also, for contributing to solving this issue.

    Not a problem at all. I’ve posted functional code that does what you describe using the site structure you posted (and a bit of guesswork as to how you build your pages). It’s kinda your call as to whether you use it or not ;-)

    As for your 404 problem, the following index.php code works for me:

    < ?php
    $page = !isset( $_GET ) ? ‘home’ : $_GET;
    $inc = file_exists( dirname(__FILE__) . ‘/theme/default/pages/’ . $page . ‘.php’ ) ? ‘theme/default/pages/’ . $page . ‘.php’ : ‘theme/default/pages/404.php';
    include( ‘theme/default/common/header.php’ );
    include( $inc );
    include( ‘theme/default/common/footer.php’ );
    ?>

    The files in ‘theme/default/pages’ simple have their title in h1s. I have files in that directory called: 404.php, about.php, contact.php, home.php, portfolio.php, resume.php.

    header.php:

    < !DOCTYPE html>

    < ?php

    include( ‘theme/default/common/navbar.php’ );

    ?>

    navbar.php:

    footer.php:

    htaccess:

    RewriteEngine On
    Options +FollowSymlinks

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

    If you are saving this in a subdirectory, change

    RewriteBase /

    to

    RewriteBase /subdirectory/

    Could you try that out first and see if it works? It’s the same functionality as the zip file I posted, but with fewer lines of code. I feel as though I solved your problem a while back but your more recent posts indicate you haven’t really read my code.

    # April 3, 2013 at 11:07 am

    Hi there

    I have a solution to your problem:

    In your .htaccess file ensure you have the following:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L,QSA]

    Then go to your index.php and add the following:

    list($path) = explode('?', $_SERVER);
    //Remove script path:
    $path = substr($path, strlen(dirname($_SERVER))+1);
    //Explode path to directories and remove empty items:
    $pathInfo = array();
    foreach (explode('/', $path) as $dir)
    {
    if (!empty($dir))
    {
    $pathInfo[] = urldecode($dir);
    }
    }

    if (count($pathInfo) > 0)
    {
    //Remove file extension from the last element:
    $last = $pathInfo[count($pathInfo)-1];
    list($last) = explode('.', $last);
    $pathInfo[count($pathInfo)-1] = $last;
    }

    Do a print_r on $pathInfo will show you everything within the URL

    Now you make all your URLS point:

    http://localhost/

    /index/

    # April 3, 2013 at 11:09 am

    Sorry the above should be:

    http://localhost/<dir>/index/<page&gt;

Viewing 10 posts - 61 through 70 (of 70 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".