This is a small guide on how to create a website where the url of pages can be custom defined within a database.
A common reason for implementing when you would like database driven pages such as a blog or a product page to have human readable urls. e.g. scorchsoft.com/news?id=3 doesn't look very nice and isn't great for SEO. We would much rather that it reads as scorchsoft.com/my-unique-page-name.
For this guide, I am going to assume that we are trying to create a very crude blog that has aliases associated with the blog posts. You can reapply this concept to other implementations.
Things we will need to do first:
- Set up a database with some text that will be displayed on the page and a unique alias such as "my-unique-page-name".
- Set up a php page that takes in either an id or an alias as a $_GET parameter and then uses it to display the data on the page.
- Set up a page to list the posts in whatever way you please.
Next: Redirecting!
The basic concept is to take all url requests and divert them through a single page so that we have full control of what is returned to the user. To do this we need to implement .htaccess rewriting as follows: (Note: your apache server must have mod_rewrite enabled)
.htaccess File
RewriteEngine On
# This is the base directory of the site.
RewriteBase /
#All pages will rewrite to this url
RewriteRule ^$ /index.php L
# No rewriting will be done here if the file exists
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
#All pages will rewrite to this url
RewriteRule .* /index.php L
So now any request to a page that doesn't exist will resolve at index.php. Now all we need to do it tell index.php to read the url alias and use it to get the correct page.
The index.php page
The index.php page needs to read the url and use it to get the page that corresponds to it.
index.php
//I like to turn on errors so that I can see what is going on if all goes wrong.
ini_set('display_errors', 1);
error_reporting(E_ALL);
//connect to the database or die if this fails
$db = mysql_connect("localhost",$username,$password)or die( "Unable to connect to database");
mysql_select_db("my_posts",$db) or die( "Unable to select database");
//this gets everything after www.mysite.com/
//so if the url was www.mysite.com/my-page then page would = "my-page"
//the escape string function prevents sql injection attack
$page = mysql_real_escape_string($_SERVER'REQUEST_URI',$db);
// if $page = /something/something-else then I only want to retrieve the text after the last forward slash
$alias = str_replace('/','',strrchr( $_SERVER'REQUEST_URI' , '/' ));
if($alias == "")
//if there is no alias then let's display the list of posts
include 'list-of-posts.php';
else
//this page should use the $alias variable in a database query to get the page content
include 'get-post.php';
?>
And then that is pretty much it. Assuming that get-post.php and list-of-posts.php have been written correctly your site will now display pages based on their custom alias.