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

You Might Also Like...

Share the Love

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

Comments

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

FriendAdder @ 1 Feb 2011 08:14:12 PM

is there any way to say: add some extra words to a url using mod_rewrite?

Example:

change:
detail.php?id=$2

to:
pressure-valve (normal mod_rewrite url)

to:
search-friendly-intro-to-pressure-valve

This would append to all urls in a certain section of a site , like product pages.
POSSIBLE?? Please send answer to my email. Thanks !

Mumesymn @ 20 Oct 2012 04:32:42 AM

best for you [URL=http://www.cheap-louisvuitton.net/]cheap louis vuitton handbag[/URL] for less ojZKKhsr [URL]http://www.cheap-louisvuitton.net/[/URL]

FluefMub @ 24 Oct 2012 01:55:38 PM

sell [URL=http://www.cheap-livingroomsets.com/category/modern-living-room-sofa-sets/]living room sofa sets[/URL] suprisely yNIJSrXa [URL=http://www.cheap-livingroomsets.com/category/living-room-tables-sets/ ] http://www.cheap-livingroomsets.com/category/discount-modern-living-room-sets-for-cheap/ [/URL]

capspipsy @ 5 Feb 2013 12:35:42 AM

TmsXev http://syanerubaggu.com/ BdoLoi [url=http://syanerubaggu.com/#51386]シャネル 店舗 コスメ[/url] XcrGiv http://syanerunihon.com/ WcwJgo [url=http://syanerunihon.com/#01568]シャネル バッグ 新作 2012[/url] UzaPwn http://ninnkisyaneru.com/ YdvKai [url=http://ninnkisyaneru.com/#99350]シャネル 化粧品[/url] OdpZmd http://syanerutennpo.com/ XooNdb [url=http://syanerutennpo.com/#93368]シャネル 店舗 コスメ[/url] ClhG FtrDaa http://toumibaggu.com/ VaoXci HyqKmz WpnN [url=http://toumibaggu.com/]TUMI 96141[/url] JixK HraLty http://toumisaihu.com/ AmgLpa BpuRlu FmjA [url=http://toumisaihu.com/]トゥミ[/url] TjcW VyoGvz http://suguretoumi.com/ FlkLcg EcdDis ZtdK [url=http://suguretoumi.com/]tumi[/url] McaF SohZtu http://ninnkitoumi.com/ AgbIoj IxaRni PtaN [url=http://ninnkitoumi.com/]TUMI 26141 DH alpha[/url]

epirllace @ 5 Feb 2013 12:35:55 AM

PbqAux http://suguresyaneru.com/ QmwYou [url=http://suguresyaneru.com/#41293]シャネル 時計[/url] HvhUyu http://yichibansyaneru.com/ CyjBft [url=http://yichibansyaneru.com/#68549]シャネル バッグ チェーン[/url] QdqDxu http://syanerusaisin.com/ DxlRla [url=http://syanerusaisin.com/#62541]シャネル 財布[/url] BhzBba http://syanerumanzoku.com/ IdhNbt [url=http://syanerumanzoku.com/#95577]シャネル 財布 コピー[/url] SntYgi http://kaidokusyaneru.com/ WkmJhd [url=http://kaidokusyaneru.com/#56458]シャネル 店舗 大阪[/url] QqoVjp http://syanerugekiyasu.com/ DyuKsj [url=http://syanerugekiyasu.com/#55159]シャネル 財布 新作 2012[/url] CgkXse http://dendousyaneru.com/ HxzEvz [url=http://dendousyaneru.com/#01299]シャネル 財布 ピンク[/url] ObyEhu http://baggusyaneru.com/ IzdRoc [url=http://baggusyaneru.com/#98431]シャネル 財布[/url]

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 to Blog

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.

Subscribe by: RSS | Email
Copyright © 2013 Supadupa Web Design