lucaBiagini.com

There is more to life than increasing its speed

iPhone version. Yes, I got it. And you too.

Posted: Wednesday, March 26th, 2008
Previous post: « Customize Wordpress Comments
Next post: Bootcamp: Airport on Windows with Parallels »

Php, MySql, .htaccess: friendly urls

There are a lot of tutorials that show how to change an address like:

1. www.mysite.com/products.php?product_id=1234

into others like:

2a. www.mysite.com/products/1234
2b. www.mysite.com/products/1234.html

In this article we’d like to go one step forward and create something like:

3a. www.mysite.com/easy-to-remember-product-name
3b. www.mysite.com/easy-to-remember-product-name.html.

How-To and Contraindications

Switching from an address like 1. to 2a or 2b is search engine necessary, because SE spiders often don’t crawl correctly our dynamic urls. Switching to 3a or 3b is much better, because it’s a search engine friendlier, and it’s human friendly too.

Now, we think we all can agree that we should change our dynamic urls into 3a or 3b, but the goal is to make it without modifying, or at least without modifying too heavily our web application. If our php script works with a query for product_id, and product_id is the unique, autoincremental key of our mysql database, we have to rewrite our urls without losing the possibility for our script to still work in its original way.

That is the way Wordpress works: a tool that allows the user to choose the preferred format for the urls. In this tutorial, we replicate - in a very simplier way - that tool.

How the system works

Our solution - with downloadable example - uses php and mysql to manage the content and create the urls, and Apache’s server mod_rewrite module to rewrite the addresses. We assume to be on a linux server and that our app is php and mysql based.

We have a 2 tables in our mysql db:

News table contains the articles, with the columns id, title, article and slug, where slug contains the sanityzed title, that is a title like: “That’s an easy mod_rewrite tutorial” becomes “thats-an-easy-mod-rewrite-tutorial”.
The script, normally, queries the db for the id, but we, through php, will find the related slug. Then we rewrite the url through mod_rewrite.

Options table contains id, name and value of the options, that is, in this example, permalinks yes/no. If permalinks are off, php will query the db for the id, and the url format will be: index.php?id=x. If permalinks are on, php will query mysql for the slug, and mod_rewrite will rewrite the url in format: rewritten-url-with-php-and-mod-rewrite.

Example

A working example is available at:
gruppomodulo.it/lavori/esempi/mod-rewrite-php-htaccess-tutorial/

Also downloadable in .zip format at:

gruppomodulo.it/lavori/esempi/mod-rewrite-php-htaccess-tutorial/ex.zip

Example includes following files:

.htaccess
Very simple .htaccess for mod_rewrite, that transforms the url from ?slug=rewritten-url to /rewritten-url.
If permalinks are off, url format will be ?id=x, and there will be no rewrite.

options.php
The file with the basic tool to switch on/off the permalinks

single.php and index.php
Show site contents.

functions.php
Contains the function that generates the urls (rewritten or not, depending on permalinks selected status) and the function that gets the articles from mysql database.

Zip file also includes the .sql file to recreate the example datatabase.

Tags: , , , , ,

This entry was posted on Wednesday, March 26th, 2008 at 11:33 am and is filed under Php & MySql, Seo & Sem, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “Php, MySql, .htaccess: friendly urls”

  1. victor Says:

    Excelent Tuto!!!

    Amazing, clean explication!
    i´ll hope works for me,

    Thanks

  2. Tom Says:

    This is a great alternative to the way I was going about this previously.

    Quick Question… Do you have a function that will sanitize the titles to input the appropriate slugs?

  3. Tom Says:

    Do you know of a way to sanitize the title to create an appropriate slug?

  4. Luca Says:

    Hi Tom,

    I’ve written several functions to do that, but in the end I’ve decided that the built-in one in wordpress is better. I’ll write a post in the next weeks. Meanwhile, I suggest you to use their function.

    Download wordpress, open wp-includes/formatting.php and clone/edit the following functions:

    - sanitize_title_with_dashes
    - remove_ accents
    - seems_utf8
    - utf8_ uri_encode

  5. Tom Says:

    Thank you sir. I found these functions on another site as well and I will look into them. Sorry about the double post, I didn’t realize your comments were moderated :). You have been very helpful!

Leave a Reply