Grow your CSS skills. Land your dream job.

PHP Switch/Case and the Dreaded Unclean URLs

  • # March 19, 2013 at 2:24 pm

    I just wanted to see if by posting this, I could get a response that actually fixes the listed issues with using Switch/Case.

    **Problems**

    **1.** How does one take these very unattractive URLs that are outputted by switch/case and make them display clean (seo friendly)?

    > Update: Problem Number 1 Fixed. Much thanks to CrocoDillon.

    **2.** Once accomplished, how does one change the active Navigation link to remain highlighted?

    > Update: Problem Number 2 Fixed. Much thanks to CrocoDillon and BenWalker.

    **Part 1.)** I have always practiced the use of switch/case (php) with my websites. Back in the not too distant past it wasn’t such a problem to have this, but in today’s modern expectations, not only by the search engine crawlers, but giving out a specific link to your colleague is very frustrating when having to provide them with something such as _http://www.mysite.com/index.php?page=mypage_.

    Of course I am talking about the following:

    < ?php

    if (!isset($_GET))
    $_GET = null;

    switch ($_GET) {

    case “home”:
    include(‘mydir/home.php’);
    break;

    case “about”:
    include(‘mydir/about.php’);
    break;

    case “contact”:
    include(‘mydir/contact.php’);
    break;

    // Default Page
    default:
    include(‘mydir/home.php’);
    break;
    }

    ?>

    The use of switch/case will output the links as follows:

    **_http://www.mysite.com/index.php?page=home_**

    **_http://www.mysite.com/index.php?page=about_**

    **_http://www.mysite.com/index.php?page=contact_**

    So, I understand that this can be cleaned up through the use of the .htaccess file, however, I have tried many ways and still no luck. So the question I have is this:

    How do I get this link:

    **_http://www.mysite.com/index.php?page=home_**

    to turn into

    **_http://www.mysite.com/home_**

    **Part 2.)** Once this has been accomplished, how does one get the main navigation to display the currently active link/page as highlighted?

    Example: Say I go to the about page by clicking on the about navigation link, while on the about page that link should show an active state. Any thoughts?

    Note: I have seen a few things on this site, by Chris that explain this, but what I have found is mostly for WordPress and I am not using WordPress. Also, I want to ensure that the ‘home’ navigation link is highlighted as active upon going to the website initially.

    If I could get some help with this/these nightmare(s) it would be greatly appreciated… Thanks in advance!

    # March 19, 2013 at 3:02 pm

    Part 1: Not a star on rewrite rules but this should work…

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

    Basically rewrites `/home` to `/index.php?home` if the requested url is not a file (-f) or directory (-d). `[NC,L]` means it’s case insensitive and the last rule to apply. You can get the value `home` from `$_SERVER`. You can probably use this value to highlight your currently active link too.

    # March 19, 2013 at 3:09 pm

    Yeah that’s what I currently have, but for some reason that takes me to my “WampServer Configuration Page”… :/

    # March 19, 2013 at 3:12 pm

    What does your full url look like when you develop locally?

    # March 19, 2013 at 3:16 pm

    For the links I have:

    http://pastebin.com/gB2uHSDH

    # March 19, 2013 at 3:27 pm

    I mean in the browser bar, http:// and then?

    # March 19, 2013 at 3:30 pm

    With case and switch I have:

    http://127.0.0.1/rpotterjr/index.php?page=about

    With Mod Rewrite:

    http://127.0.0.1/rpotterjr/about

    # March 19, 2013 at 3:37 pm

    I think that’s the issue… it redirects to the root which is `http://127.0.0.1`, so `http://127.0.0.1/index.php?home`. Set up a vhost like `http://rpotterjr/` or `http://rpotterjr.dev/` or something so your site is at root level.

    # March 19, 2013 at 3:41 pm

    In `C:WindowsSystem32driversetchosts` add `127.0.0.1 rpotterjr.dev`

    In `C:Program Fileswampbinapacheapache2.2.22confextrahttpd-vhosts.conf` add


    ServerAdmin webmaster@localhost
    DocumentRoot “C:pathtoyourpublicfolder”
    ServerName rpotterjr.dev
    ErrorLog “logs/rpotterjr-error.log”
    CustomLog “logs/rpotterjr-access.log” common

    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

    Then restart all services from the wampserver menu.

    # March 19, 2013 at 4:01 pm

    For some reason when I use http://rpotterjr.dev it takes me to the “WampServer Configuration Page”… Did both of what you said to do…

    # March 19, 2013 at 4:12 pm

    Hmmm I had the same issue once, but forgot how I fixed it. I’ll look into it.

    # March 19, 2013 at 4:33 pm

    Can’t seem to find it :( Try Google how to set up virtual hosts on Apache because it will definitely fix the .htaccess Rewrite issue.

    # March 19, 2013 at 4:37 pm

    Ok, nevermind. I setup the Virtual Host and now it is redirecting me to my home page. The address in the address bar is changing, but the page content is NOT.

    # March 19, 2013 at 4:40 pm

    In other words, the address changes through:

    rpotterjr.dev/about

    and

    rpotterjr.dev/contact

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

    # March 19, 2013 at 4:45 pm

    If I revert back to not having the rewrite, I can navigate from page to page changing the content, but the address displays as http://rpotterjr.dev/index.php?page=about

Viewing 15 posts - 1 through 15 (of 70 total)

You must be logged in to reply to this topic.

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