The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Programming Fundamentals" Dec 02 - 2:00 PM Eastern

PHP Switch/Case and the Dreaded Unclean URLs

  • # March 19, 2013 at 4:47 pm

    Change `switch ($_GET) {` to `switch ($_SERVER) {`

    If that doesn’t work try `var_dump($_SERVER)` and see what you get.

    # March 19, 2013 at 4:56 pm

    No change…

    # March 19, 2013 at 5:03 pm

    So, what did `var_dump` get you?

    # March 19, 2013 at 5:04 pm

    Nothing different. I tried restarting the server and clearing browser cache…

    No change..

    # March 19, 2013 at 5:08 pm

    I was at this stage before without the Virtual Server by specifying the root in php, but couldn’t ever get past this problem…

    # March 19, 2013 at 5:13 pm

    Put `var_dump($_SERVER)` in you index.php file (before the switch), and show me what it gives with and without the .htaccess file.

    # March 19, 2013 at 5:17 pm

    Maybe you have to enable mod_rewrite in your apache install.

    # March 19, 2013 at 5:24 pm

    I have mod_rewrite enabled. As for the var_dump($_SERVER), nothing is different. Just back at square one again…

    # March 19, 2013 at 5:27 pm

    Nothing is different doesn’t give me much info to work with.

    # March 19, 2013 at 5:28 pm

    I can make the var_dump($_SERVER)

    before the switch/case in a separate statement, but all that does is display a count of the character length of each link clicked.

    # March 19, 2013 at 5:29 pm

    The address changes through:


    However, the content stays stuck on the home page’s content.

    If I revert back to not having the rewrite, I can navigate from page to page changing the content, but the address displays as This is with or without var_dump($_SERVER)

    # March 19, 2013 at 5:31 pm

    So here is my code currently:

    RewriteEngine On

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

    # March 19, 2013 at 5:33 pm

    PHP case/switch

    < ?php if (!isset($_GET))
    $_GET = null;

    switch ($_GET) {

    case “portfolio”:

    case “resume”:

    case “about”:

    case “contact”:
    // Default Page


    # March 19, 2013 at 5:33 pm


    # March 19, 2013 at 5:33 pm

    The I guess QUERY_STRING just reads the URL instead of using the internal rewriting. Try `var_dump($_GET)` (with .htaccess) to see if the ‘page’ key (it won’t be called page unless you change the .htaccess file) is in there. If it is, you can use something like `if (isset($_GET)) {…}` etc.

Viewing 15 posts - 16 through 30 (of 70 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed