SEO 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:
http://www.mysite.com/page/page.php?id=3

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. Here at EDinteractive, all the websites we develop use a content management system PHP CMS that produces friendly URL’s. If you are making your own solution however, 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: http://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:
http://www.mysite.com/page/3

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:
http://www.mysite.com/page/page.php?id=3
can now be referenced as:
page_address_identifier
http://www.mysite.com/page/page.php?id=contact_us
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:
http://www.mysite.com/page/contact_us

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: http://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:
http://www.mysite.com/contact_us

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/(.*)

Conclusion

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. If you would like to know more about this article or would like us to develop something for you,

Advertisements