Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP Switch/Case and the Dreaded Unclean URLs

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 64 total)
  • Author
    Posts
  • #43504
    rpotterjr
    Participant

    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:

    $_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!

    #128789
    CrocoDillon
    Participant

    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.

    #128791
    rpotterjr
    Participant

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

    #128792
    CrocoDillon
    Participant

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

    #128794
    rpotterjr
    Participant

    For the links I have:
    http://pastebin.com/gB2uHSDH

    #128800
    CrocoDillon
    Participant

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

    #128801
    rpotterjr
    Participant
    #128803
    CrocoDillon
    Participant

    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.

    #128804
    CrocoDillon
    Participant

    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.

    #128811
    rpotterjr
    Participant

    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…

    #128814
    CrocoDillon
    Participant

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

    #128817
    CrocoDillon
    Participant

    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.

    #128818
    rpotterjr
    Participant

    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.

    #128819
    rpotterjr
    Participant

    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.

    #128820
    rpotterjr
    Participant

    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 64 total)
  • The forum ‘Back End’ is closed to new topics and replies.