Supdadupa Web Desgn

EASY Search Engine Friendly URLs with mod_rewrite

Create search-engine friendly urls for your website with this easy to understand tutorial.

One key to getting good rankings on SERPs (Search Engine Results Page) is using mod_rewrite to make search engine friendly urls:

Example: http://www.mysite.com/keyword/this-is-loads-of-great-keywords

A novice web developer would look at the above address and think that this meant creating a directory called 'keyword' with a sub-directory of 'this-is-loads-of-great-keywords'. This novice web developer would be wrong. Using the mod_rewrite feature is required to creating urls like this, with not actually creating the page itself. Confused? So was I when I first started trying to do this. There are no easy to follow tutorials on the web which explain this feature well enough or in-depth enough to be useful. So, here is my attempt at an easy to understand yet in-depth tutorial on using mod_rewrite to create search engine friendly urls.

This tutorial is making the assumption that you are on an Apache web server which has the mod_rewrite feature enabled.

Setting up .htaccess

Firstly, we need to create a .htaccess file. Relatively speaking, this file tells the browser where to go when a url is typed in. To enable mod_rewrite in a directory, create the file .htaccess in a text editor and put the following at the top of the file:

RewriteEngine On

 

Creating Your First Rewrite Rule

Now, this is where the fun begins. In the example I'm going to be using in this tutorial, I will have a file called article.php which needs an GET variable passed to it called article_id (ex: article.php?article_id=89). I'm going to change this into a much nicer url of '/any-text-here-89'. So to do this I enter the following line into my .htaccess file:

RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$          article.php?article_id=$2 [L]

WHOA! What is all this strange syntax here? Well, its called regex (regular expression). I'm not a fan of regular expressions, its like a secondary coding language for string formatting, but it's extremely useful for this. So let me explain the above line.

We start with 'RewriteRule'. This tells the server that we want to declare a rule for a url to be rewritten if it matches the following pattern.

Next we have the ^ character. This is extremely important and must be the first thing after RewriteRule. I believe it means 'start from the end of the domain' to match the pattern.

Now we have ([a-zA-Z0-9_-]+). This is regex for 'allow any number of characters and numbers with the addition of the underscore (_) and the hyphen (-). The + on the end tells it 'any number of characters'. So 'this-is-a-string' or 'this-is-a-really-long-string' would match.

The next part is a a hyphen (-). This means it must have a hyphen after the series of characters and numbers.

After that, we have ([0-9]+). This is regex for 'allow any number of numbers'.

Then at the end we have $. This means, 'and nothing more'. It may not be apparent how important this individual character is, but when you start adding multiple RewriteRules that start becoming very similar, if you don't have this character, you will have some crazy problems if you don't put your RewriteRules in an organized order.

So, then we have a few spaces, followed by article.php?article_id=$2. Now, this is more like it. Something that resembles something in English. This tells the browser where to send the matched pattern to. So anything that matches the regex will go to article.php and pass something as aritcle_id.

The $2 tells the browser to use the second variable from the regex pattern. Each regex variable is grouped by the parentheses (()). So in this example, the second variable will be what matches ([0-9]+). So if the url ends up being 'http://www.mysite.com/this-is-an-article-89', the second variable is '89'.

It is also very important to add '[L]' at the end of each RewriteRule line. This will tell the browser to stop trying to match the url to any other RewriteRules. Not adding this can cause great problems with pages going to the wrong places.

Expanding on the Example

With this in mind you could create a more complex RewriteRule, which passes more than 1 variable. Here is an example:

RewriteRule ^category-([0-9]+)/article-([0-9]+)$           article.php?category_id=$1&article_id=$2 [L]

The above example passes 2 variables to article.php. So, if I went to http://www.mysite.com/category-9/article-448, the above rule will pass a category_id of 9 and an article_id 448 to article.php.

Conclusion and Useful Hints

Hopefully this tutorial has helped you understand mod_rewrite better. I suggest planning out your url structure before writing out these RewriteRules to avoid any problems with rules that might have duplicate matches. For reference, here are some helpers to get you on the right track with your RewriteRules:

RewriteRule ^(.*) - Matches anything after the domain (ie: www.mysite.com/php or /php.php)

RewriteRule ^(.*).html - Matches anything with a .html at the end (ie: www.mysite.com/php.html)

([0-9]+) - Matches must contain any number of numbers

([a-zA-Z0-9_-]+) - Matches any number of alphanumeric characters

Share the Love

If you liked this post, why not share it with others?

Make money on your website, quickly and easily.

Comments

soogesilutt

17 Nov 2009 06:15:03 AM
sibearian orchestra halloween music [url=http://royalmp3.net/artist8235/bill-hallett-discography/]Bill Hallett[/url] lonely planet thialand music http://royalmp3.net/artist3841/astral-projection-ft.-zehava-ben-discography/ dance not music http://royalmp3.net/artist2808/cynic-discography/
ben stiller new movie to be release in september http://moviestrawberry.com/films/film_bend_it_like_beckham/ transformers movie theme [url=http://moviestrawberry.com/films/film_fracture/]captain america movie offical site[/url] movie theater in garner nc [url=http://moviestrawberry.com/films/film_monsters_vs_aliens/]monsters vs aliens[/url]
college diego music san htm [url=http://royalmp3.net/artist24098/aidan-broadbridge-discography/]Aidan Broadbridge[/url] free music synthisizer [url=http://royalmp3.net/artist2282/beyonce-discography/]the longest yard opening music[/url]
the captain america movie pics http://moviestrawberry.com/films/film_the_other_sister/ apartment 12 movie [url=http://moviestrawberry.com/films/film_life_on_liberty_street/]requiem for a dream movie[/url] star wars movie poster [url=http://moviestrawberry.com/films/film_lucker/]lucker[/url]
audiences targeted by rap music [url=http://royalmp3.net/artist24885/sean-kingston-discography/]Sean Kingston[/url] dick bullings world of music [url=http://royalmp3.net/artist1061/heroes-del-silencio-discography/]phil collins music codes[/url]
movie induced tourism http://rusfilms.com/ joblo movie releases [url=http://rusfilms.com/page/2/]mooresville movie theater[/url]

Jason

28 Jan 2010 09:37:27 AM
Hey,

With the expanded example I tried removing the words article and category from the URL condition.

RewriteRule ^([0-9]+)/([0-9]+)$ index.php?pageid=$1&act=$2 [L]

So that when you go to http://domain.com/pageid/act
It actually sends pageid to the pageid and act to the act.

However, when I try this it just says 404 everytime. I've tried so many combinations and stumped, even though your tutorial was very good in getting me this far!

Regards,
Jason

Leave a Comment

Name (Required)
Email (Required, but not published)
Website
Comment
Get emails when comments are added. (You can stop emails at any time)

Want an Avatar? Get one at http://www.gravatar.com

Subscribe

Get instant updates.
Subscribe by: RSS | Email

About the Author

David

Hi, i'm David! I've been building websites since 1996. Through my experiences, I have gained a well-rounded knowledge of the design & development of websites. I founded Supadupa Web Design to help others learn from my experiences.

Follow me on Twitter