How to create pages with custom url aliases using php, mysql, and htaccess
This is a small guide on how to create a website where the url of pages can be custom defined within a database. This is often known as managing the url alias.
In summary
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 this is 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
1RewriteEngine On23# This is the base directory of the site.4RewriteBase /56#All pages will rewrite to this url7RewriteRule ^$ /index.php [L]89# No rewriting will be done here if the file exists10RewriteCond %{REQUEST_FILENAME} !-f11RewriteCond %{REQUEST_FILENAME} !-d1213#All pages will rewrite to this url14RewriteRule .* /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 is 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
1//I like to turn on errors so that I can see what is going on if all goes wrong.2ini_set('display_errors', 1);3error_reporting(E_ALL);45//connect to the database or die if this fails6$db = mysql_connect("localhost",$username,$password)or die( "Unable to connect to database");7mysql_select_db("my_posts",$db) or die( "Unable to select database");89//this gets everything after www.mysite.com/10//so if the url was www.mysite.com/my-page then page would = "my-page"11//the escape string function prevents sql injection attack12$page = mysql_real_escape_string($_SERVER['REQUEST_URI'],$db);1314// if $page = /something/something-else then I only want to retrieve the text after the last forward slash15$alias = str_replace('/','',strrchr( $_SERVER['REQUEST_URI'] , '/' ));1617if($alias == "")1819 //if there is no alias then let's display the list of posts20include 'list-of-posts.php';2122else2324 //this page should use the $alias variable in a database query to get the page content25include 'get-post.php';2627?>
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.
Share
Want to talk about your project?
Tell us what you’re trying to achieve and we’ll map the fastest credible path.
