Left Quote    I start where the last man left off.
- Thomas A. Edison    
Right Quote
[login] | [Register]

Mod_Rewrite URLs for Search Engines

by: bs0d
Page: 3 of 3
(View All)

The rules thus far have been for one directory. Now we need to set up a rule that will take care of the rest, because the old url was set to: (where X = the id,page of the tutorial in the database)

Instead if the URL looking like that, we're going to change it to this (for example):

The first directory after /tutorials/ is the category, followed by the tutorial id #, then the name of the tutorial and finally the page number. You can seperate category types and the names of the tutorials with whatever you wish, but if you decide to use a space, the browser will interpret that as %20. So, the Underscore (_) and dash (-) seem to be the most popular seperators.

Here is the rule for the change:

Here is the rule broken down. ^(.*)/ starts the rule for our first wild card directory after /tutorials. Followed by two others. the final is a wildcard but its not a directory. Notice that it ends with: .php$ -We're saying the final wildcard is going to be a php file. The [NC] at the end indicates no case, not case sensitive.

Now after that, we tell the rule where to get the data for the wildcard directories and file. Recall the original link: (where X = the id,page of the tutorial in the database)

So, in our rule we put: view.php?id=$2&page=$4 [NC]. We told it to access view.php file. Then, for the 2nd wildcard, insert value from id in view.php. Next, we told the rule to place the value from page in the view.php query string into the 4th wildcard directory specified.

When someone now visits:
it will be the same as :

This is your final product code:

So, where do the tutorial names come from?

Where I got the names of my tutorials were straight from my tutorials table in my database for the site, the same goes with the category. But, the names from the database will have spaces, and the browser interprets those as "%20" - so you will want to use the str_replace(); function to change them to "spaces_like_this". Or, use the php function, urlencode(); Here is an example using str_replace();

The only thing left to do, is to make sure to change your links in your files. Inside the url, you can easily echo the type, title, id - right into the link (from the database). Remember though, you probably dont want the url to have %20 for every space in the title or type, so keep urlencode(); and str_replace(); in mind, like in the example above.

Conclusion & Links

We have covered the mere basics of using mod_rewrite, but this is a good start for further progression. A good place to start after this tutorial would be to learn about rewrite conditions. I am still learning on them myself. Perhaps once I perfect the process there will be another tutorial similar to this on. So I hope that you find this tutorial useful. I know there are lots of tutorials on this topic out there today, so I am honored that you read through it and I hope you gained some knowlege from it. Below are some more resources for definitions, rules and syntax. Or just in case you still need more examples to clear some things up.

-bs0d |

1  |  2  |  3  |  

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

0 Total Comments
5 Rating of 5 (1 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



Your Article Here

"" Copyright © 2002-2020; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank