PHP Search Engine Optimization - Mod_rewrite
(Page 3 of 4 )
It's very important that you plan how to rewrite you URLs, because you don't want to go back and change the links over and over again. Once you have a good steady map of how to rewrite your links, you can go ahead and modify a test script to see if everything worked. If your links work properly then you'll be able to move on and modify all existing links. Let's take a look at this example below:
http://example.com/index.php?act=articles&id=21&page=0
That can turn into:
mod_rewrite URL: http://example.com/articles/21/0.html
Doesn't that look much friendlier?
Once you have planned your URL rewriting, you are ready to set up your .htaccess file, which maps out what to do with each URL. Here's an example of what to put in your .htaccess file. I'll explain how this works below.
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*).html /index.php?act=$1&id=$2&page=$3
In the example above we have two lines. The first line RewriteEngine On starts the mod_rewrite engine.
The second line does all the work.
RewriteRule ^(.*)/(.*)/(.*).html /index.php?act=$1&id=$2&page=$3
Here you are starting a RewriteRule.
RewriteRule ^(.*)/(.*)/(.*).html /index.php?act=$1&id=$2&page=$3
This is the start of a regex that allows you to create wildcards for the URL that your friendly URL. This allows us to use whatever we want between the (.*) and compare them to the second part of this rewrite rule.
RewriteRule ^(.*)/(.*)/(.*).html /index.php?act=$1&id=$2&page=$3
This is the final part of the rewrite rule that tells us how to map the friendly URL in part two of the RewriteRule to the actual URL that our script was written for. mod_rewrite will translate part one to part 2 automatically.
This works by using regex. The first (.*) will be $1 and the second (.*) becomes $2 in the translation. You can do this as many times as you want for your URL.
Say for example we had this URL: articles/21/0.html.
We'll be matching up /index.php?act=articles&id=31&page=0 by using mod_rewrite!
Believe it or not, that's all that's really to mod_rewriting!
You can really get carried away with this by adding category titles to some of your rewrite URLs to make them even more search engine friendly. All you have to do is create another regex and don't use that variable in your translation.
Next: ForceType >>
More Search Optimization Articles
More By Roger Stringer
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|