How to use mod_rewrite with .htaccess (Part 1)
Update - I wrote the continuation to this article here “How to use mod_rewrite with .htaccess (Part 2)” go check it out after you’re done with this one.
So maybe some of you have seen a website with URLs like this:
http://www.crainbandy.com/index.php?action=login
Or perhaps you’ve seen this
http://www.crainbandy.com/login but you’re just not sure how they get all those get variables and .php’s and things into one pretty little string? Well, I’m going to show you how to do just that.
First off you need to have Apache and the mod_rewrite module uncommented in the httpd.conf if you’re not sure open your config file and check, in /etc/httpd/conf/http.conf (I’m running Fedora linux) If you can’t find it just do a locate httpd.conf in the bash and you should be fine.
Find the line where apache loads all the modules, you’ll know because you’ll start to see a lot of LoadModule going around. Some config files will vary depending on what version of apache your running but we’re trying to achieve the same goal here so, just make sure you remove the comment (#) from the rewrite_module modules/mod_rewrite.so
It should look like this:
Load Module rewrite_module modules/mod_rewrite.so
and you’re set as far as that’s concerned.
Now we’re going to be using .htaccess to specify the mod_rewrite rules so we’ll need to make sure you’re allowing .htaccess. Look for the flag “AllowOveride All” and make sure it says just that. Then look for a flag called “AccessFileName .htaccess” These flags must be set otherwise it’s not going to work.
.htaccess just allows you to control file/folder permissions on a per-folder basis instead of overall. It comes in handy quite often, such as right now. Okay, hope you’ve got it…here we go.
I am not going to go into a huge amount of depth here (after all, it’s just part 1) but I do want to leave you with something to play with so you can make your sites look pretty and nice. If you need to do more than we do in this (you probably won’t, most average people only need a few mod_rewrite rules) then stay tune for part 2.
First things first, create a .htaccess file in your web root directory (wherever your index.html or index.php (hopefully) file is at) Inside this file, we’re going to enable mod_rewriting and make a few rules.
Lets enable mod_rewriting in our .htaccess this is done simply by putting,
RewriteEngine on
That did it! Apache is now waiting for rules to pass when certain variables are met, simple enough aye?
Now, lets add a rule. So lets say you’re using your index.php file to do all the work. It displays your page, it takes variables, it does everything. Lets say you have a GET command similar to this URL http://www.crainbandy.com/index.php?cmd=login Well then simply put, it’s obviously for logging in; but, what if you want it to be just http://www.crainbandy.com/login ? that is MUCH prettier isn’t it? Well we can do that to with mod_rewrite, of course.
Basically mod_rewrite takes what I call the “base” URL (which is what you see in your address bar) and translates it into the “real” URL, which is what PHP and the rest of the web monsters can read and actually do stuff with. Here we go.
# This is a comment in a .htaccess file.
# Tell Apache we’re about to specify a mod_rewrite rule
# Then give the query string to rewrite
RewriteRule ^login?$ index.php?cmd=login
So in a nutshell, it takes the base URL /login (http://www.crainbandy.com/login) which is fine and good, but it SENDS (this is the important part) it SENDS PHP and everything else the second line which is index.php?cmd=login, it’s just a simple substitution which proves for a much prettier browse.
Think of it like a translator. Not a lot of people understand ?cmd=login&passive=true&return_url=/home so why not just rewrite it so that all you have to get is a /login string? Much prettier, don’t you agree? By the way, all that voodoo you see in the beginning like ^ and ?$ is a thing called Regex. It basically says “^” (start the rule) & “?$” (finish the rule)
So cool you say, cool cool. So now I can rewrite static URLs but that does nothing for dynamic variables; duh, of course you know I’m going to show you how to do that too. So what if you’re not sure what they’re going to ask for, but there is a huge number of possibilities and you don’t want to have to write them all out? I like the way you think. Here we go.
So lets say you have more than one ?cmd and you have SEVERAL, you have register, login, add, forward, message, reply, just a buttload of commands and you don’t want to make RewriteRules for all of them, well we can take advantage of Apache’s dynamic variable system (Which rocks) So here it goes
# Again, we have to tell apache to turn on mod_rewriting
RewriteEngine On# Now, lets make that dynamic rewriting!
RewriteRule ^cmd/([^/]+)?$ index.php?cmd=$1
What the sdflkjafsjkl is all of that %&* )#@($()@ non-sense you ask?! That’s Regex my friend, he should be your BEST friend. I’ll explain more about that later but that statement in a NUTSHELL basically tells Apache “Anything after a slash” So now you can accept any commands you want with this new base URL, http://www.crainbandy.com/cmd/ANYTHING_YOU_WANT.
Take notice in the $1 (Apache is so smart, he knows since we specified regex and a sort of “catch all” regex, to create a variable for us on the fly. How sexy is that?)
Well, that should get you started.
Peace & Kittens.
bryan
Related reading
4 Responses to “How to use mod_rewrite with .htaccess (Part 1)”
Leave a Reply

lmthong on October 2nd, 2006
I did as this but it seems nothing “rewrite”.
viggz on April 23rd, 2008
trying to redirect mydomain.com/folder -> http://www.mydomain.com/folder can’t get it to work…. :/
Michal Popielnicki on May 9th, 2008
You have a spelling error:
“AllowOveride All” instead:
“AllowOverride All”
Best regards.