Welcome to TQ CMS knowledge base. Here you will find useful information for all sorts of things.

Friendly url using php and htaccess

This is the problem - you have a website or content management system that works happily, but the addresses that it produces are something like this:

This is fine - but have you considered the following:
1. This may not be the most search engine friendly address, particularly if it is long and unwieldy.
2. It is not so easy to give out specific addresses to people say for a conference or a specific project or product.
3. It could be more secure to prevent hacking such as SQL injection attacks etc.

So what could you do to improve the situation?

Two main things spring to mind - the quick way for starters would be to lose the page.php bit - this is quite straightforward - you just need to do the following:

1. Create a folder called ‘page’ (or whatever you like)
2. Rename page.php to index.php and move this file into your newly created folder
3. Voila - your new address will now be: www.mysite.com/page/?id=3
That is a little bit better - but what if you wanted to lose those nasty question marks, ampersands and even numbers?

There are several ways to do this, but the one this article covers is to modify your .htaccess file for the website. If you do not have access to this file, or are unfamiliar with it - then this can open a can of worms - so here is a few simple things you can do to achieve completely friendly url's using this method.

Using .htaccess to produce friendly url's - Intro to .htaccess

The .htaccess file is a very powerful configuration file and should be used wisely as it can affect everything and anything about your website. First of all, check that you are able to create and use a .htaccess file on your site. On the root top level of your site, you should have this file and if not, you should be able to create a file in notepad and call it .htaccess. This in itself can be tricky - you need to make sure the file is called .htaccess and not .htaccess.txt or whatever. You can edit this file in Dreamweaver (or your editor) though if you like by right clicking it and choosing: Open with > Dreamweaver

Configuring .htaccess for friendly url's

You need to put the following lines into your .htaccess file:

RewriteEngine On
RewriteRule /page/(.*)$ /page/page.php?id=$1

This basically tells your website that any request for your page should be re-written. Doing this will mean that your addresses will become further simplified to the following:

That is great isn't it? Or is it? What happens if you have a conference or a product to promote - this address is still not friendly enough for your marketing campaign or to add more words into your search engine ranking - so how could you further improve this address? Here is one approach that you could use, but it involves a fairly serious change to your site, so think carefully before doing this.

Here comes the science - moving from id number identifiers to letters

This is the tricky bit. Your site so far has been using id numbers to identify its pages. The method covered in this article is going to change all that to use words to identify a record in the database. Firstly - create a new field in the database called something like page_address_identifier and set this to allow characters.
Each record in your database will then need some unique words to identify each record. So for example, page.php? id=3 was id 3 in the database, so I'll add my new field to the database and set its value to be something like: contact_us
This identifier should use underscores_ instead of spaces and should ideally use lowercase characters a-z and/or 0-9. In the admin section of your site also - it may be a good idea to validate this field and check for duplicates to make sure your page_address_identifier's are in the correct format and are unique.
You will also need to alter your SQL statement on page.php to select a record from the page_address_identifier field of your database and not the old id field.

Once this is done, it now means that:
can now be referenced as:
Not very friendly though is it?

That's where the two techniques come together. Because of the changes you made to your .htaccess file earlier, you can now magically use this address:

Between the two techniques, contact_us is retrieved from the page_address_identifier field and .htaccess strips out page.php?id= 

That is the hard part done - you now have totally friendly url's. There are yet more things you can do to make your addresses even more friendly if you so desire. Imagine for example you wanted this address: www.mysite.com/contact_us
This is quite possible, but again it could potentially open up a can of worms. In order to do that, you could change your .htaccess file to the following:

RewriteEngine On
RewriteRule (.*)$ /page/page.php? id=$1

Simple isn't it? Well, actually, no it isn't - you should now theoretically have this address:

But it doesn't work for some reason - you will most likely get an internal server error, because this sends the server back and forth in an infinite loop. In order to get around this problem, you need to add this following condition to your .htaccess file before your rewrite rule:

rewritecond %{REQUEST_URI} !^/page/(.*)

This essentially allows the page folder to function. You'll also have to add a line for each top level folder on your website that you want to allow e.g.:

rewritecond %{REQUEST_URI} !^/page/(.*)
rewritecond %{REQUEST_URI} !^/images/(.*)
rewritecond % {REQUEST_URI} !^/documents/(.*)


This article has covered several methods to make dynamic website addresses friendlier for your users and the search engines. There are obviously many ways you could do this, but your choice of method will ultimately depend on how brave you are feeling, what rights you have and how friendly you want your url's to be. Modifying .htaccess can be a frustrating business at times to troubleshoot, but hopefully this article and the many others out there should help you achieve the addresses that you would like.

There is 57 guides in the database.

More news

25 February, 2014


I have implemented bootstrap into the project and changed all code in the administration to use this new framework and I really love the result that I see after a couple of hours of coding. To make things look so good has never been so easy as with
20 February, 2014


I have added some of my guides for different things when it comes to the different areas in the computer, I will continue to add guides and especially when it comes to things that I am using every now and then in my own work.
First phase completed
17 February, 2014

First phase completed

Then I had time to finish the dirty work with the page and now it only remains little touches here and there and I need to upload information about what I'm doing and my progress on the project as well.
Time for a change
16 February, 2014

Time for a change

Today I've decided to update my own personal website with the latest version of my project called TQ CMS and with a completely new design that is responsive. My goal with this project is to make it as easy as possible for the end user while


The next step

The next step

I have now resumed my programming after a couple of months of vacation which was very much needed. I got a call one day from a friend that needed a system which can handle peoples reports on various problems like kitchen problems, water problems etc and I decided to make a system for it. Little did I know that it would totally revolutionise my own project and that it would teach me much needed jquery skills to further enhance the CMS. I also