// Tutorials / Manipulating URLs with mod_rewrite

Posted The: 04/09/2008 - AT: 9:56

This Tutorial covers how to enable mod_rewrite to redirect, or rewrite requested URLs.

Why use it

Simplifying your URLs can make it easier for your users to share your resources, as well as easier for some search engines to index your pages. It also solves the unencoded ampersand problem, which may trigger warnings in the validation of your markup.

How it works

The below example will redirect URLs from the Articles/NUMBER/, to the real URLs. This redirect happens on the server, and the user wont notice anything.

RewriteEngine on
RewriteRule ^Articles/([0-9]+)/$ /?AID=$1

First i enabled the RewriteEngine by setting it to "on", next i typed in my rewrite rule, which is a simple regular expression.

The Regular Expression

Take the below.

^Articles/([0-9]+)/$ /?ARTICLE=$1

The ([0-9]*) part telles that the numbers 0-9 are allowed, and the following plus sign (+) tells that one or more is allowed, finally the parentheses is used to remember the content, this can later be accessed through the variable "$1", which will be used to pass the number on to the real URL. Basically this tells the server to rewrite the below root-relative URLs.

/Articles/NUMBER/

Into the following (root relative).

/?ARTICLE=$1

If you dont understand what i mean about root relative, then you should also read Absolute and Relative Paths.

Problems

One problem which is likely to be common for beginners, is broken URLs for their external stylesheets, and/or images. Mod_rewrite and relative paths, are known to cause problems, the solution is either to use absolute paths, or root relative paths. You should read Absolute and Relative Paths for more information.

Stumble It!

Author: BlueBoden

Comments: [0]

  1. HTML Tutorial
  2. CSS Tutorial
  3. XHTML Tutorial
  4. RSS Tutorial
  5. PHP Tutorial
  6. htaccess Tutorial
  7. Server Tutorial
  8. SEO Tutorial

Checked At:01:32

2 Guests

0 Users

Online doing the past 15 Minutes!

PageRanking.dk - Flere hits og højere PageRank - GRATIS tilmelding!

This page was created in 0.0953409671783 seconds

Welcome Guest