Tipsglobe Logo Facebook Share Twitter Share Google Share LinkedIn Share StumbleUpon Share

Why to rewrite your URL`s ?
The main reason is because static URL`s (the second URL is static) are indexed more faster than dynamic URL`s (like the first one), because from a static URL is easier to understand what the page is about both for search engines and your visitors. When using dynamic URL`s there is an another problem: search engines are reading only the beginning of the long URL`s, so if your addresses are different only at the end, than search engines will see the same URL for all of your pages and from this reason none will be indexed.

How this works ?
We will use Apache mod_rewrite. The mod_rewrite transforms back the second URL into the first URL, so the server will access the "ugly" address, but the visitors and the search engines will see the "clear" one. mod_rewrite simply rewrites the URL`s what meets specific conditions into addresses what are understood by the server.

Let`s rewrite it!
The code what rewrites the website address needs to be placed in your .htaccess file (if you don`t have it in your root folder, create it), the code is very short:
RewriteEngine on
RewriteRule ^tutorials/(.*)/(.*)-([0-9]+)/?$ /tutorials.php?id=$3&title=$2&category=$1

Take it apart!
The first line: RewriteEngine on simply does what it says, it starts the rewrite engine.
Now let`s take apart the second line:
RewriteRule - this tells the server about the rule to follow when rewriting the URL
^ - this exponential sign means the start of the URL:
tutorials - this word is simply added to the website adress
/ - this character separates the directories
(.*) - this tells the server that here will be placed some data (any kind of characters) (e.g.: php)
- - this character will be simply added to the website adress
([0-9]) - this tells the server that here will be placed a digit (e.g: 1)
+ - the plus sign tells the server that here can be more digits (e.g.: 123)
? - this sign tell the server that the character in it`s front it`s not required (so the URL will work with and without the last "/")
$ - the dollar sign tells the server that here is the end of the rule
The last part of the line is the old address, the value of each variable (id, title and category) are changed to $3, $2 and $1. mod_rewrite takes these variables and places them in their new position in the new URL. In the first place for data (the first (.*)) it places the $1`s value (the category value), in the second place the value of $2 and so on. If you want to change the order just change the $1, $2 and $3 order, but be careful to set the right data type in the new URL.

I know that it`s not easy, if you have any question please don`t hesitate, ASK ME! And I will try to answer as fast as I can. You can for example write me in a comment your URL and how you want to transform it and I will try to make it ;)

Post a Comment Blogger