PHP: Switch navigation with mod_rewrite
In this tutorial I will show you how to make PHP switch navigation with mod_rewrite work for your site, it will help your rankings in search engines by making your URLs search engine friendly, and it will make it easier for your users to understand where they are on your site by simply looking at the URL.
Before your start this tutorial you should check that your web server has mod_rewrite installed. To do this make a blank PHP file and add the following code to the file:
Upload the file and open it in your browser. What you should see is the PHP installation info on your server. It shows all the settings and modules installed with PHP. Scroll down and make sure the mod_rewrite module is listed. If it's not mod_rewrite will not work on your server and you should contact your web host and ask them to install it for you.
Now that you are sure you have mod_rewrite installed on your server lets start with the actual tutorial.
PHP switch navigation is widely used for it's simplicity of use with dynamic content systems where pages, tutorials, downloads, etc. are added to the site using online forms, however it leaves ugly URLs that are very unfriendly to search engines. Here is an example of what a PHP switch navigation URL might look like:
With mod_rewrite we can have the same dynamic URL but also have it be easy for your visitors to read/understand and search engine friendly. Here is an example of what that URL might look like with mod_rewrite:
Now that you know what we will be doing and what you need it's time to get started with the code. The first thing will be the main page that will transfer the user to all the other pages. This should be your index.php as this is the page the user will access if he/she just types in your domain. We will also include our header and footer template files, this will keep the layout uniform throughout the site and make it easy to change it on the whole site. Here is the code:
//Include page header
include('header.php');
//Get the page variable
$page = $_GET['page'];
//Our switch statement to get the right content
switch($page) {
case "home": //For each page of content just add a case block
$content = "home.php"; //The file with the page contents. DO not include the header and footer HTML in these files it is already added.
break; //Stop the case block, if you do not have this you will get an error.
case "news":
$content = "news.php";
break;
default: //If the variable didn't match any of the above cases do this.
$content = "home.php";
break;
}
//Include the selected content.
include($content):
//Include page footer
include('footer.php');
?>
That’s basically the whole script. When you want to add a page you need to add another 'case' block to the switch statement in the code above and then make the file. To point to that file you need to use the following URL: http://www.yoursite.com/index.php?page=YOURPAGENAME, don't worry we will fix it later to http://www.yoursite.com/page/YOURPAGE.html. Now you just need to make header.php and footer.php with the HTML before and after the content. You also need to make the content pages, here are the example files:
header.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>My cool site!</title>
</head>
<body>
<a href="index.php?page=home">Home</a> | <a href="index.php?page=news">News</a>
<b>Here is the page content:</b><br>
footer.php
home.php
news.php
That's the PHP/HTML part of your script! Now we need to make a .htaccess file that will activate the mod_rewrite and make our URLs pretty. Make a new file and call it .htaccess and here is the code you need to put in it:
RewriteRule /page/(.*).html index.php?page=$1
All this does is it takes a URL like /page/news.html and makes it changes it for the server into index.php?page=news. And that’s about it! All you need to do now is change all your links to the new format and you will have pretty, and search engine friendly URLs!
This is a basic PHP navigation script, it is a fully working one, but it is an example to show you can use to improve upon and write your own script fitted perfectly for your site. Your are free to use this script and you are encouraged to improve it for your own purposes.
If you have any questions or comments leave them using the form below and if you liked this tutorial, please think about registering at Design Avarice.
January 13th, 2007 at 10:35 pm
[...] Register « PHP: Switch navigation with mod_rewrite [...]
March 23rd, 2007 at 3:04 am
A very nice tut! I newbie like me learn a lot in switch PHP pages ^__~
March 23rd, 2007 at 3:05 am
I mean. learned a lot, not learn a lot.. heheh ^__~
March 23rd, 2007 at 3:05 am
I mean. learned a lot, not learn a lot in switching PHP pages.. heheh ^__~
March 23rd, 2007 at 3:05 am
I mean. learned a lot, not learn a lot, and switching not switch.. whew! im so sleepy na waaa!! heheh ^__~
March 23rd, 2007 at 3:08 am
Sorry for reposting my comments, I didn't do it on purpose.. it was my browsers fault
March 24th, 2007 at 9:02 pm
lol!
Glad you liked the tutorial,
thanks for reading!
March 26th, 2007 at 3:51 pm
it helped me a lot as a php newbie hepa7. thanks
April 1st, 2007 at 9:21 pm
A great simple tute for a beginner to php like myself.
April 2nd, 2007 at 3:46 pm
I'm glad you liked it!