简体   繁体   中英

why isn't index.php working?

I have a single index.php page that should link to the rest of my websites.

<?php 

    if(!isset($_GET['page']))$page = 'home.php';
        else{
            $page = $_GET['page'] . ".php";
        }

    include("_includes/header.php");

    include("_includes/navigation.php");

    include("_pages/$page");

    include("_includes/footer.php");
?>

This is what my index.php looks like. It's supposed to get the page i click on (when i clikc on the navigation) and direct it to this page so that the header, navigation, and footer will be reused for every page.

<div id="navi">
    <ul>
        <li><a href="home">Home</a></li>
        <li><a href="skills.php">Skills</a></li>
        <li><a href="_pages/my_projects.php">Projects</a></li>
        <li><a href="_pages/experience">Experience</i></a></li>
        <li><a href="_pages/personal">About</a></li>
        <li><a href="_pages/contact">Contact</a></li>
    </ul>
</div>

according to the code the "home" link should work but it says URL not found because it is looking for "home" in the directory that I'm in. however its in my pages directory. "skills" does the same thing. the only ones that do work is the links projects through contact because i am specifying a path. however, the links that work don't go through my index page because the header.php (which contains the css), navigation.php, and footer.php is not included here.

i did research and i found that i had to do something with the .htaccess file and/or something with mod_rewrite. can someone please help me out. I've been looking for about 2-3weeks now and nothing.

You should be referencing index.php, not home..

<div id="navi">
    <ul>
        <li><a href="index.php?page=home">Home</a></li>
        <li><a href="skills.php">Skills</a></li>
        <li><a href="_pages/my_projects.php">Projects</a></li>
        <li><a href="_pages/experience">Experience</i></a></li>
        <li><a href="_pages/personal">About</a></li>
        <li><a href="_pages/contact">Contact</a></li>
    </ul>
</div>

Your links should be:

<div id="navi">
    <ul>
        <li><a href="index.php?page=home">Home</a></li>
        <li><a href="index.php?page=skills">Skills</a></li>
        <li><a href="index.php?page=my_projects">Projects</a></li>
        <li><a href="index.php?page=experience">Experience</i></a></li>
        <li><a href="index.php?page=personal">About</a></li>
        <li><a href="index.php?page=contact">Contact</a></li>
    </ul>
</div>

It's strectly related on how your web server handles requests. If you try to visit url that's not handeled by PHP you won't get it to work. By deafult PHP handles only .php request url (also .php5 and some others).

A solution could be to write a rewrite condition that convert your /home/ to index.php?page=home. This assuming you're running Apache Web Server with mod_rewrite enabled and htaccess enabled.

So, supposing to normalize your links this way:

  <div id="navi">
      <ul>
            <li><a href="/pages/home">Home</a></li>
            <li><a href="/pages/skills">Skills</a></li>
            <li><a href="/pages/my_projects">Projects</a></li>
            <li><a href="/pages/page=experience">Experience</i></a></li>
            <li><a href="/pages/page=personal">About</a></li>
            <li><a href="/pages/page=contact">Contact</a></li>
      </ul>
  </div>

you can write an .htaccess like this, and put it in /pages/ folder:

   RewriteEngine On
   RewriteRule ^/pages/(.*)$ /index.php?page=$1 [QSA,L]

Beware that every request to /pages/ will be rewrited, so you should write them carefully avoiding rewrite of .css .png and so on...

This will effect your request this way:

  1. Apache will recive request for a file in /pages/ named home
  2. RewriteRule will detect it with the regexp
  3. The rule forces apache to answer with your index.php with the parameter page=home (or whatever the regex has catched)

PS Maybe you could be interested in flag P to send request to index.php as a Proxy, but for further infos you should refer to mod_rewrite docs ( http://httpd.apache.org/docs/current/mod/mod_rewrite.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM