How to generate friendly URLs with .htaccess


Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2398

Warning: implode() [function.implode]: Argument must be an array in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3351

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3415

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: Invalid argument supplied for foreach() in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Warning: Invalid argument supplied for foreach() in /home/triqui/public_html/emanueleferonato.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Ok, now you have your own game portal. Let’s call it triqui.com.

You want to share a link with your friends, or want search engine to index it properly.

If I want you to play Jamag, I have to give you this link
http://www.triqui.com/play.php?id=1713.
Now, I would like you to tell me how can you understand I am talking about Jamag from this link http://www.triqui.com/play.php?id=1713.
You can’t.

Now let’s understand why I have to write that play.php?id=xxxx to play a game.

All information about the games is stored in a database, and every game has an unique id assigned by the script.

Jamag’s id is 1713, so when I pass this value, the php script knows where to retrieve information about the game.

If you want to play Jamag on Kongregate, this is the link:
http://www.kongregate.com/games/triqui/jamag.

Seems like Kongregate has a directory to store my games (triqui) and a subdirectory for every game I made.

Obviously that’s not true. This is possible thanks to…

.htaccess file

Normally .htaccess is used to implement custom error pages or password protected directories. But you can do a lot more with this file.

First, let me point that .htaccess is the file extension, not the filename. The filename does not exists.

That’s what I created with my favourite text editor (notepad…)

1
2
3
ErrorDocument 404 /index.php
RewriteEngine on
RewriteRule ^id/([^/\.]+)/?$ /play.php?id=$1 [L]

Let’s see what is it:

Line 1: ErrorDocument detects any document error. If you don’t know what I am talking about, here it is a brief list:

401: Unauthorized – The request requires user authentication.

403: Forbidden – The server understood the request, but is refusing to fulfill it.

404: Not Found – The server has not found anything matching the Request-URI.

So ErrorDocument 404 refers to a “not found” error… that happens when the user looks for a page that does not exist

/index.php is the path where to redirect the user if he requested a page that does not exist

We can say that ErrorDocument 404 /index.php means “if someone requested a page that does not exist, then redirect him to index.php”.

And that’s what happens: go to http://www.triqui.com/dfgretre (a page that does not exist) and you will be redirected to home page.

Just in one line of code…

Line 2: Activation of the RewriteEngine module, a rewriting engine to rewrite requested URLs on the fly.

Not all servers support the rewrite engine.

In order to determine if your server supports it, you have to upload a php file with just:

1
2
3
<?php
phpinfo();
?>

and see what happens. In the result page, search for “mod_rewrite”.

If you find it in the “Apache loaded modules” section, then you know your server supports the rewrite engine, although in a server with php version 4.4.7 I was able to make it work even if the search returned a negative result.

Line 3: The core instruction: RewriteRule looks if the current URL matches with the regular expression passed as first parameter.

That ^id/([^/\.]+)/?$ is a regular expression, and even if I am planning to make a tutorial about regular expressions (reg exps from now on), at the moment I am just recommending you this Wikipedia article.

The second parameter of RewriteRule is the substitution.

Basically this instruction says: if the page starts with id (^id/) then copy everything that’s not a slash, a backslash or a period (([^/\.]+)) and make sure that after the starting matched id there is only a slash (/?$)… then load the page /play.php?id=$1, pasting the characters previously copied in place of $1.

The final [L] tells not to search for any more rule if this one was satisfied.

In an even basic way, the script says:

If you find an URL like
http://www.triqui.com/id/1713/
just redirect to URL
http://www.triqui.com/play.php?id=1713

And that’s what happens if you go to http://www.triqui.com/id/1713/.

I hust had to upload that .htaccess file in my root directory.

You may say there is not a big difference between play.php?id=1713 and /id/1713/ but this is the first step to friendly URLs generation.

Stay tuned for the next update.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.60 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 13 comments

  1. RipeX

    on June 11, 2008 at 1:11 am

    Really awesome! :D

  2. Robin

    on June 11, 2008 at 12:16 pm

    Oh god, thank you. Really been looking for this :D

  3. Shawn

    on June 11, 2008 at 5:20 pm

    You describe this stuff in such a great and simple way that I have no problem following along and I’m not a programmer. I can’t wait for the update! Thanks again for a great tutorial!

  4. CrociDB

    on June 11, 2008 at 9:05 pm

    Very good tutorial! Thank you! :)

  5. Livesinabox

    on June 12, 2008 at 12:51 am

    I was working on this very thing the other day for mochi publisher – probably using the slug field. I think it’s unlikely to make much of a difference to the player. This is usually whats called search engine friendy urls for dynamic content sites. I doubt any player will type in your game url directly, but it will look a bit better when you place a link that is just a url rather than a description (but then i think it’s always better to provide a descriptive text name instead of just the address and looks better). However, how much difference it actually make with regards to rankings i don’t know, i guess that would depend on the name of the game and the keywords used.

  6. How to generate friendly URLs with .htaccess - part 2 : Emanuele Feronato - italian geek and PROgrammer

    on June 15, 2008 at 1:52 pm

    [...] you read part 1, you should know how to have a friendly url to play a game in your Flash game [...]

  7. Robin

    on June 26, 2008 at 1:59 pm

    Hm, it says ‘server made a boo boo’. What’s wrong?

  8. Neghina Cristi

    on August 20, 2008 at 3:28 am

    Very good , very good tutorial.
    Really been looking for this.
    10x.

  9. Klas

    on June 14, 2009 at 4:04 pm

    Nice but i wonder how you do like this

    http://www.triqui.com/id/1713-text

    please

  10. Zigmas

    on June 1, 2010 at 7:25 pm

    Hey, I was wondering, why all my images go out after I write anything after slash/ ?
    I only can write http://www.triqui.com/id and if i do
    http://www.triqui.com/id/something all images just goes out :/

  11. Anonymous

    on March 18, 2011 at 4:47 pm

    Would this code work? It’s supposed to turn http://www.example.com/games/publisher/thegame into http://www.example.com/play.php?name=thegame&creator=publisher.

    <codeErrorDocument 404 /index.php
    RewriteEnging on
    RewriteRule ^games/([^/\.]+)/([^/\.])/?$ /play.php?name=$1?creator=$2 [L]

  12. Anonymous

    on March 18, 2011 at 4:49 pm

    Sorry, the last post was messed up.


    ErrorDocument 404 /index.php
    RewriteEnging on
    RewriteRule ^games/([^/\.]+)/([^/\.])/?$ /play.php?name=$1?creator=$2 [L]

  13. superposts

    on May 8, 2011 at 5:11 am

    it did not work for me :(