London SEO
London SEO

03
Sep

A basic introduction to mod_rewrite.

A basic introduction to mod_rewrite.

What is mod_rewrite?
Mod_rewrite is a system for apache that lets you basically rewrite urls. It is quite advanced, while at the same time being quite simple.

Why is it so important?
It lets you change pages from something like viewnews.php?id=329579823948 to something like /viewnews/the-news-story-headline/. Of course, you will need to update your actuall links (from id=… to the new ones). But the reason it is so good is because firstly, who can remember 329579823948? And who can remember the-news-story-headline? Additionally, it is a lot better for seo to have keywords in the url, and most times your page title / etc will have the keywords in it.

Ok, walk me through a simple example using mod_rewrite
Ok. We are going to do a very simple mod_rewrite first, one that doesn’t involve any parameters (no id= or whatever).

For example, say at the moment our page is:
http://example.com/about_us.html

We want it to be
http://example.com/about/

Firstly you will need to be using apache. Make a file called .htaccess, and put it in your public html folder (same folder as index.php/index.html etc etc. In my case with my host, it would be /london-seo.com/.htaccess. Often it is something like public_html). You may have a bit of trouble making it in Windows. If you do, name the file “.htaccess (with the quotation marks).

Now you wanna put in .htaccess the following (first two lines are not always needed, depending on your server configuration):

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^about/$ about_us.html [L]

See, its pretty easy. The ^ just means that represents the start of the string, and $ represents the end of the string. It is just regular expression stuff… You don’t need to worry about it too much for the time being. Note how the domain itself doesn’t matter (nothing about example.com is in there)

Now lets do a more complicated example. We want to change:
http://example.com/news.php?newsid=48672

to:
http://example.com/news/48672/

Well we would then put in the following code:

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^news/(.*)/$ news.php?newsid=$1 [L]

The $1 indicates that it should replace whatever was in the first occurrence of (.*) (note - there can be more than one (.*).)

Well, that’s it for a basic mod_rewrite tutorial. Ideally you don’t wanna have id’s that relate just to a number, but you want some keywords. A good way to do this is to in your news.php only use the newsid=xx, but add in some extra, so you could have:

http://example.com/news/mod-rewrite-tutorial/89487/

The code for that would be:

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^news/(.*)/(.*)/$ news.php?newsid=$2 [L]

Notice how there is no “$1“. As you get more advanced you will get used to this kinda stuff. Again, this is only a basic tutorial, there is a lot more to it, but this will get you started.


1 Comment »

  1. […] October 2, 2006 at 6:38 am · Filed under Web Development From: http://london-seo.com/a-basic-introduction-to-mod_rewrite/18/trackback/A basic introduction to mod_rewrite. […]

    Pingback by Ali’ Blog » A basic introduction to mod_rewrite. — October 2, 2006 @ 3:38 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Privacy Policy Terms and conditions of use of this site