Mod Rewrite For pretty urls

I am useless when it comes to mod_rewrite and htaccess files. I can do the basics but nothing more.

Please help :slight_smile:

I am looking to forward a url in the format of

into the following url

Can anybody help me out? Even with a good tutorial?

A good place to start is

If you have any question after (or while) reading that, please ask :slight_smile:

The problem I am currently having is that I do not know how to handle the last part of the url where one or more params can be sent in.

This part: “&param1=value1&param2=value2…”

You can’t write a rule for an arbitrary number of parameters.
You can either write several rules for different number of parameters, or redirect all URL’s to one script and let the script figure it out.

Thanks ScallioXTX!

I am now redirecting all urls to my bootstrap file and handling the params in the php. :slight_smile:

Actually, not true! That’s EXACTLY what the optional atoms do! As with functions with default values, though, the order is important as, to have the last atom, you must have all preceding atoms, too.

test/dotest/param1/value1/param2/value2 … to

Let me assume that the test/dotest is required as is at least the first key/value pair:

^test/dotest/([a-z]+)/([a-z]+)(/([a-z]+)/([a-z]+)(/(([a-z]+)/([a-z]+))?)? index.php?m=test&a=dotest&&$1=$2&$4=$5&$7=$8

That must ALL go on a single line!

Do you see the repeating pattern? If so, then it is possible to combine the repeating pattern and use a metacharacter for counting the number of key=value pairs. Unfortunately, the number of Apache variables you may create in this manner is limited to 9 but you can chain ([C]) these as many times as is necessary (to eat up your 255 character URL limit). This, however, I will leave to you.

To make the regex code clearer:




… m & a are italic
… $1 & $2 are magenta
… $3 is DarkRed and optional and includes the / separator and includes the $6 atom as well as the $4 & $5 atoms
… $4 & $5 are red
… $6 is Red and optional and includes the / separator and includes the $7 & $8 atoms
… and $7 & $8 are dark red



  1. I ↩︎

Yes, you can also do that, but it’s not very readable and doesn’t support more than 9 variables, which is why I suggested the front controller :slight_smile: