SEO friendly URLs with htaccess and PHP include script

This is part one of the guides SEO-friendly URLs with .htaccess.
In the next part I’ll describe how you can use this technique on dynamic URLs from a database, ie. news articles etc.

I’ve always used PHP include script to include my websites subpages.
However, it doesn’t look too good when the URL says “”.
It looks much better as “”.
In this guide I’ll show you how to use PHP include script and combine it with SEO friendly URLs.

To create SEO friendly URLs all you need is a little file called .htaccess.

Step 1
Open up your text editor and type the following:

RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1

Save this as .htaccess and upload to your webserver.
If you type, the Apache web server will now interpret it as

Step 2
The SEO friendly URLs should work now, but you also need a PHP script that will include the subpages.
The following script is set to include all pages from a directory called “inc”, but change it to whatever you like.

Note: This should be in your index.php file where the main content appear.

$page = $_GET['page'];

$def = "home";
$dir = "inc";
$ext = "php";

if (isset($page)) {
$page = substr(strtolower(preg_replace('([^a-zA-Z0-9-/])', '', $page)), 0, 20);
if (file_exists("$dir/$page.$ext") and is_readable("$dir/$page.$ext")) {
else {
else {

Step 3

Change all internal links and replace them with your new SEO links.
For example, if you have a link to index.php?page=about, change this to about.

You’re finished! Open your website and try to enter the short SEO friendly URL.
Please let me know if you encounter any problems.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s