getting apache mod_rewrite to work (Part 1)

Send to friendPrinter-friendly version

We've all heard about it, but there are a lot of us that cannot get it to work. I couldn't get mod_rewrite to work for a very long time, but i managed to figure it out in the end, thanks to noel (www.noelharrison.net) and drupal (i had a peak inside their .htaccess file.

so what does mod_rewrite do? in a nutshell, it takes query strings (such as ?q=blah&something_else=more) and turns it into something beautiful (such as /blah/something_else/more), this makes it look like a file structure and allows people to enter URLs manually much easier.

lets get our hands dirty. mod_rewrite will not work by default, there are some server modifications that you have to make, and also u have to be running apache. if you are running on shared hosting, such as hetzner or godaddy they should have it turned on by default but if ur not sure please pop them an email and ask them.

I am gonna show u how to configure a linux server running apache2 to use mod_rewrite. i will be doing this example as if im working on Red Hat/Fedora/CentOS - please comment or email me if you need some help or are running on a different distro.

i am also assuming that you are a little bit linux savvy and that you are fairly comfortable working with the linux shell. if you arent please dont worry too much, you should be able to follow quite easily, if you find it difficult please email me and i will endevour to help you. :)

so lets start with the apache configs:

login to the server (i am working as tho through a terminal session, so if you are using a GUI please open a terminal/shell) and then open the apache conf file (i am using emacs, but u can use vi as well, or any text editor)

emacs /etc/httpd/conf/httpd.conf

Once you have opened the conf file please look for a line saying;

LoadModule rewrite_module modules/mod_rewrite.so

if you do not find a line like that in the conf file please add it exactly like it is above.

then there is one more thing that we have to do in the conf file:

Directory /var/www/html/

inside that directory tag you should see an option like this:

AllowOveride None

it might have All instead of None, but if it has None please change it to All.

after that you must exit from the text editor and you must restart apache like this:

service httpd reload

and that should be fine. In Part 2 we will look at actually implementing Mod_rewrite on your site/appl.

Jonathan Wagener is a web developer specialising in PHP/MySQL as well as Drupal consultant and developer. He is also fluent in both XHTML and CSS design. Espresso Online is the online playground of Jonathan where he posts interesting news articles and also writes bits and pieces.

Jonathan currently works at Amoebasys (www.amoebasys.com), a web development and VoIP company.

mod

ya. Mod rewrite seems reasonably easy and makes urls look a lot nearer, my new cms makes use of it on the front end.

Noel

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options