AdRock Posted July 16, 2007 Share Posted July 16, 2007 I have Apache, PHP and MySQL all installed on my PC i use at home to test websites as I build them. Apache is running fine as well as the rest of it but i have a problem when using rewritten URLs. On the actual host itself the pages work fine when i use the navigation but on this machine it doesn't work properly. For example of i click this link <a href="/contact">Contact</a>, on the host the page is displayed correctly but on my local machine it displays only the called page without displaying the index page first ( I have used require to call the page to be included within the index page) I have enabled mod rewrite in the apache.conf file so don't know why it's not displaying properly <?php //disable all error reporting error_reporting(0); $url=$_SERVER["REQUEST_URI"]; $urlLength=strlen($url); //First check if home page if($urlLength == 0) $mainMenu = "HOME"; else { //remove leading and trailing / $url=trim($url,"/"); //split into 2 parts (incase this is a 2 level link) $url=split("/",$url); //assign first link to mainMenu $mainMenu=strtoupper($url[0]); //assign second link to subMenu if it exists if(isset($url[1]))$subMenu=strtoupper($url[1]); } //create array of pages $pages=array( "HOME" => "home.php", "ARCHIVED-ARTICLES" => "archive_articles.php", "ARCHIVED-EVENTS" => "archive_events.php", "ARCHIVED-NEWS" => "archive_news.php", "ARCHIVED-HONEYLANDS NEWS" => "archive_honeylands_news.php", "ARTICLES" => "articles.php", "CONTACT" => "contact.php", "COMING-EVENTS" => "events.php", "EVENTS-GALLERY" => "events_gallery.php", "HONEYLANDS" => "honeylands.php", "HONEYLANDS-HISTORY" => "history.php", "HONEYLANDS-GALLERY" => "honeylands_gallery.php", "HONEYLANDS-NEWS" => "honeylands_news.php", "WHAT-IS-HYDROCEPHALUS" => "hydro.php", "GALLERY" => "gallery.php", "JACK" => "jack.php", "JACKS-GALLERY" => "jack_gallery.php", "NEWS" => "news.php", "POEMS" => "poem.php", "SPONSORS" => "sponsors.php", "TOY-LIBRARY" => "toy.php" ); //set section page title if(isset($subMenu))$title=ucwords(str_replace("-"," ",strtolower($subMenu))); else $title=ucwords(str_replace("-"," ",strtolower($mainMenu))); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>Jack Godfrey Honeylands Support Fund</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta> <meta name="description" content="The Jack Godfrey Honeylands Support Fund aims to raise money to by organising a variety of events and prize draws to help Honeylands Childrens Centre purchase a new toy library"></meta> <meta name="keywords" content="jack, fund raising, jack godfrey, honeylands, children, jack godfrey honeylands support fund, Honeylands childrens centre, disabilities, exeter, Exeter, Exeter Devon, Honeylands Exeter, Pinhoe, pinhoe, honeylands exeter, honeylands near exeter"></meta> <script type="text/javascript" src="/includes/js/expandingMenu.js"></script> <link href="/includes/css/jack.css" rel="stylesheet" type="text/css"></link> <link href="/includes/css/gallery.css" rel="stylesheet" type="text/css"></link> <link href="/includes/css/menu.css" rel="stylesheet" type="text/css"></link> </head> <body> <div id="wrapper"> <div id="title"><img src="images/title.jpg" alt="Jack Godfrey Honeylands Support Fund banner logo" /></div> <div id="time"><p style="text-align:right; font-family: Verdana, Arial; font-size:9pt;"> <?php print date ('l j F Y'); ?> </p></div> <?php if(isset($_SESSION["username"])) { echo ("<div id=\"login\"><h3>Welcome ".$_SESSION["username"]."</h3></div>"); } ?> <div id="nav"> <div id="adam"> <select name="jumpit" onchange="document.location.href=this.value"> <option selected="selected">Navigation</option> <option value="/adam/jack/index">Home</option> <optgroup label="About"> <option value="/jack">Jack</option> <option value="/honeylands">Honeylands</option> <option value="/history">Honeylands History</option> <option value="/toy">The Toy Library</option> <option value="/hydro">What is Hydrocephalus?</option> </optgroup> <optgroup label="News"> <option value="/news">News / Events</option> <option value="/honeylands_news">Honeylands News</option> <option value="/articles">Articles</option> </optgroup> <option value="/events">Coming Events</option> <optgroup label="Archive"> <option value="/archive/news">News / Events</option> <option value="/archive/honeylands_news">Honeylands News</option> <option value="/archive/articles">Articles</option> <option value="/archive/events">Events</option> </optgroup> <option value="/gallery">Image Gallery</option> <option value="/poem">Poems</option> <option value="/gbook/gbook.php">Guestbook</option> <option value="/adam/jack/sponsors">Sponsors / Supporters</option> <option value="/adam/jack/contact">Contact Us</option> </select> </div> </div> <div id="main"> <div class="news"> <div class="heading"><h3 class="head"><img src="images/nav.jpg" alt="Navigation" class="head" />Navigation</h3></div> <ul id="menu"> <li><a href="/index">Home</a> </li> <li>About <ol> <li><a href="/jack">Jack</a></li> <li><a href="/honeylands">Honeylands</a></li> <li><a href="/history">Honeylands History</a></li> <li><a href="/toy">The Toy Library</a></li> <li><a href="/hydro">What is Hydrocephalus?</a></li> </ol> </li> <li>News <ol> <li><a href="/news">News / Events</a></li> <li><a href="/honeylands_news">Honeylands News</a></li> <li><a href="/articles">Articles</a></li> </ol> </li> <li><a href="/events">Coming Events</a></li> <li>Archive <ol> <li><a href="/archive/news">News / Events</a></li> <li><a href="/archive/honeylands_news">Honeylands News</a></li> <li><a href="/archive/articles">Articles</a></li> <li><a href="/archive/events">Events</a></li> </ol> </li> <li><a href="/gallery">Image gallery</a></li> <li><a href="/poem">Poems</a></li> <li><a href="/gbook/gbook.php">Guestbook</a></li> <li><a href="/sponsors">Sponsors / Supporters</a></li> <li><a href="/contact">Contact Us</a></li> </ul> </div> <div class="news"><div class="heading"><h3 class="head"><img src="images/users.jpg" alt="User menu" class="head" />Membership</h3></div> <ul class="menu2"> <li><?php if(isset($_SESSION["username"])) { echo "<a class='one' href='/my_profile'>My Profile</a>"; } else { echo "<a class='one' href='/register'>Register</a>"; } ?> </li> <li> <?php if(isset($_SESSION["username"])) { echo "<a class='one' href='/logout'>Logout</a>"; } else { echo "<a class='one' href='/login'>Login</a>"; } ?> </li> <?php if ($_SESSION['user_level'] == 3) { echo "<li><a href='http://jackgodfrey.org.uk/admin/'>Admin</a></li>"; } ?> </ul> </div> <div class="news"> <div class="heading"><h3 class="head"><img src="images/news.jpg" alt="Latest News" class="head" />Latest News</h3></div> <?php //include_once("includes/connection.php"); //$counter = 1; //$q = mysql_query("SELECT id, title FROM news ORDER BY id DESC LIMIT 5"); //echo "<ol class=\"blah\">"; //while($row = mysql_fetch_array($q)) //{ //$title = htmlentities($row['title']); //echo "<li><a class='one' href='/news/".$counter."'>".$title."</a></li>"; //$counter++; //} //echo"</ol>"; ?> </div> <div class="news"> <div class="heading"><h3 class="head"><img src="images/news.jpg" alt="Latest Articles" class="head" />Latest Articles</h3></div> <?php //include_once("includes/connection.php"); //$counter = 1; //$q = mysql_query("SELECT id, title FROM articles ORDER BY id DESC LIMIT 5"); //echo "<ol class=\"blah\">"; //while($row = mysql_fetch_array($q)) //{ //$title = htmlentities($row['title']); //echo "<li><a class='one' href='/articles/".$counter."'>".$title."</a></li>"; // $counter++; //} //echo"</ol>"; ?> </div> <div class="news"> <div class="heading"><h3 class="head"><img src="images/news.jpg" alt="Useful Information" class="head" />Useful Information</h3></div> </div> <div class="news"> <div class="heading"><h3 class="head"><img src="images/info.jpg" alt="Current Fund" class="head" />Current Fund</h3></div> <?php $myFile = "includes/thermometer/testFile.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo "<img src=\"includes/thermometer/thermo.php?current=10000&max=25000\" alt=\"Current Fund Amount\" style=\"padding-left:70px\" />"; ?> </div> <div class="news"> <div class="heading"><h3 class="head"><img src="images/news.jpg" alt="Useful Websites" class="head" />Useful Websites</h3></div> <ul class="menu2"> <li><a href="http://www.escapesupportgroup.com">ESCAPE Support Group</a></li> <li><a href="http://hydranencephaly.com/">Rays of Sunshine</a></li> <li><a href="http://www.cafamily.org.uk/Direct/h53.html">Contact a Family</a></li> </ul> </div> </div> <div id="content"> <? function generate_breabcrumb($mainMenu, $subMenu) { $mainMenu=strtolower($mainMenu); $subMenu=ucwords(str_replace("-"," ",strtolower($subMenu))); if($mainMenu == "home") $breadcrumb = ""; else if(strlen($subMenu) == 0) $breadcrumb = "<p id=\"breadcrumb\"><a href=\"/index\">Home</a> > ".ucwords(str_replace("-"," ",$mainMenu))."</p>"; else $breadcrumb = "<p id=\"breadcrumb\"><a href=\"/index\">Home</a> > <a href=\"/{$mainMenu}/\">".ucwords(str_replace("-"," ",$mainMenu))."</a> > $subMenu</p>"; echo $breadcrumb; } //generate breadcrumb if(!isset($subMenu))generate_breabcrumb($mainMenu,""); else generate_breabcrumb($mainMenu,$subMenu); //Check to see if we are on a submenu page if(isset($subMenu)) { //on submenu page so include it switch($subMenu) { case "EVENTS-GALLERY": require($pages["EVENTS-GALLERY"]); break; case "HONEYLANDS-GALLERY": require($pages["HONEYLANDS-GALLERY"]); break; case "JACKS-GALLERY": require($pages["JACKS-GALLERY"]); break; default: require($pages["HOME"]); break; } } else { //include main menu page cos not sub menu switch($mainMenu) { case "HOME": require($pages["HOME"]); break; case "ARCHIVED-ARTICLES": require($pages["ARCHIVED-ARTICLES"]); break; case "ARCHIVED-EVENTS": require($pages["ARCHIVED-EVENTS"]); break; case "ARCHIVED-NEWS": require($pages["ARCHIVED-NEWS"]); break; case "ARCHIVED-HONEYLANDS NEWS": require($pages["ARCHIVED-HONEYLANDS NEWS"]); break; case "ARTICLES": require($pages["ARTICLES"]); break; case "CONTACT": require($pages["CONTACT"]); break; case "COMING-EVENTS": require($pages["COMING-EVENTS"]); break; case "EVENTS-GALLERY": require($pages["EVENTS-GALLERY"]); break; case "HONEYLANDS": require($pages["HONEYLANDS"]); break; case "HONEYLANDS-HISTORY": require($pages["HONEYLANDS-HISTORY"]); break; case "HONEYLANDS-GALLERY": require($pages["HONEYLANDS-GALLERY"]); break; case "HONEYLANDS-NEWS": require($pages["HONEYLANDS-NEWS"]); break; case "WHAT-IS-HYDROCEPHALUS": require($pages["WHAT-IS-HYDROCEPHALUS"]); break; case "GALLERY": require($pages["GALLERY"]); break; case "JACK": require($pages["JACK"]); break; case "JACKS-GALLERY": require($pages["JACKS-GALLERY"]); break; case "NEWS": require($pages["NEWS"]); break; case "POEMS": require($pages["POEMS"]); break; case "SPONSORS": require($pages["SPONSORS"]); break; case "TOY-LIBRARY": require($pages["TOY-LIBRARY"]); break; default: require($pages["HOME"]); break; } } ?> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
trq Posted July 16, 2007 Share Posted July 16, 2007 Have you actaually written any rewrite rules? Would you like to show us them? PS: This code is very bloated. For instance, this... <?php switch($mainMenu) { case "HOME": require($pages["HOME"]); break; case "ARCHIVED-ARTICLES": require($pages["ARCHIVED-ARTICLES"]); break; case "ARCHIVED-EVENTS": require($pages["ARCHIVED-EVENTS"]); break; case "ARCHIVED-NEWS": require($pages["ARCHIVED-NEWS"]); break; case "ARCHIVED-HONEYLANDS NEWS": require($pages["ARCHIVED-HONEYLANDS NEWS"]); break; case "ARTICLES": require($pages["ARTICLES"]); break; case "CONTACT": require($pages["CONTACT"]); break; case "COMING-EVENTS": require($pages["COMING-EVENTS"]); break; case "EVENTS-GALLERY": require($pages["EVENTS-GALLERY"]); break; case "HONEYLANDS": require($pages["HONEYLANDS"]); break; case "HONEYLANDS-HISTORY": require($pages["HONEYLANDS-HISTORY"]); break; case "HONEYLANDS-GALLERY": require($pages["HONEYLANDS-GALLERY"]); break; case "HONEYLANDS-NEWS": require($pages["HONEYLANDS-NEWS"]); break; case "WHAT-IS-HYDROCEPHALUS": require($pages["WHAT-IS-HYDROCEPHALUS"]); break; case "GALLERY": require($pages["GALLERY"]); break; case "JACK": require($pages["JACK"]); break; case "JACKS-GALLERY": require($pages["JACKS-GALLERY"]); break; case "NEWS": require($pages["NEWS"]); break; case "POEMS": require($pages["POEMS"]); break; case "SPONSORS": require($pages["SPONSORS"]); break; case "TOY-LIBRARY": require($pages["TOY-LIBRARY"]); break; default: require($pages["HOME"]); break; } } ?> Could be replaced by.... <?php if (isset($mainMenu)) { require $pages[$mainMenu]; } ?> There are many many more aspects that could be improved upon too. Not dissing, just pointing out some stuff. generally if you find yourself typing out lots of simular code, your doing something wrong. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 16, 2007 Share Posted July 16, 2007 Not sure if you know this, but I noticed you use both types of PHP tags (<?php ?> and <? ?>). The latter tags, also known as short tags won't work by default when you install PHP. I generally try to stick to using the full tags. Using the full tags will allow your code to be more portable over different PHP setups. To make short tags work, either change your short tags (<? ?>) to full tags (<?php ?>) or enable the setting called short_open_tag within the php.ini. Quote Link to comment Share on other sites More sharing options...
AdRock Posted July 16, 2007 Author Share Posted July 16, 2007 Thank......i didn't know that especially about the huge switch statement Anyway here is my rewrite rules Options +FollowSymLinks RewriteEngine on RewriteBase / RewriteRule ^admin$ admin/ [R,L] RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [NC,L] RewriteRule ^/?([a-zA-Z_]+)/([0-9]+)$ index.php?page=$1&pagenum=$2 [NC,L] RewriteRule ^/?([a-zA-Z_]+)/([0-9]+)$ index.php?page=$1&id=$2 [NC,L] RewriteRule ^/?gallery/jack$ index.php?page=jack_gallery [NC,L] RewriteRule ^/?gallery/events$ index.php?page=events_gallery [NC,L] RewriteRule ^/?gallery/honeylands$ index.php?page=honeylands_gallery [NC,L] RewriteRule ^/?gallery/jack/image/([0-9]+)$ index.php?page=image&cat=jack&pagenum=$1 [NC,L] RewriteRule ^/?gallery/events/image/([0-9]+)$ index.php?page=image&cat=events&pagenum=$1 [NC,L] RewriteRule ^/?gallery/honeylands/image/([0-9]+)$ index.php?page=image&cat=honeylands&pagenum=$1 [NC,L] RewriteRule ^/?archive/news$ index.php?page=archive_news [NC,L] RewriteRule ^/?archive/news/([0-9]+)$ index.php?page=archive_news&pagenum=$1 [NC,L] RewriteRule ^/?archive/honeylands_news$ index.php?page=archive_honeylands_news [NC,L] RewriteRule ^/?archive/honeylands_news/([0-9]+)$ index.php?page=archive_honeylands_news&pagenum=$1 [NC,L] RewriteRule ^/?archive/events$ index.php?page=archive_events [NC,L] RewriteRule ^/?archive/events/([0-9]+)$ index.php?page=archive_events&pagenum=$1 [NC,L] RewriteRule ^/?archive/articles$ index.php?page=archive_articles [NC,L] RewriteRule ^/?archive/articles/([0-9]+)$ index.php?page=archive_articles&pagenum=$1 [NC,L] RewriteRule ^/?admin/([a-zA-Z_]+)$ /admin/index.php?page=$1 [NC,L] RewriteRule ^/?admin/([a-zA-Z_]+)/([0-9]+)$ /admin/index.php?page=$1&id=$2 [NC,L] RewriteRule ^/?forum/([a-zA-Z_]+)$ /forum/login.php?sid=$1 [NC,L] RewriteRule ^/?forum/([a-zA-Z_]+)$ /forum/profile.php?mode=$1 [NC,L] Quote Link to comment Share on other sites More sharing options...
AdRock Posted July 17, 2007 Author Share Posted July 17, 2007 I have included a screenshot of what happens when i click this link <a href="/contact">Contact Us</a> so in the url window it appears as http://localhost/contact and a screenshot of the the page when the i open localhost to show http://localhost Like I said i don't repeat the menu etc so i just include the page using that switch. BTW i tried changing the code to <?php if (isset($mainMenu)) { require $pages[$mainMenu]; } ?> and it doesn't display a default page [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.