mod_rewrite Part 2

Send to friendPrinter-friendly version

in part 1 (www.espresso-online.info/site/node/66) I talked about some server setup stuff that is required before you can use mod_rewrite. If you are hosting on a shared hosting platform you do not really need to read part one as the server should already be setup.

I just need to say that i am working on a system that uses Apache 2.x, PHP 5.x, and Fedora Core 8.

Ok, to the more interesting stuff, for a start you need to create a .htaccess file in the root file of your site/application. A .htaccess file is required as it is used as an extension to the Apache configuration file, which allows you to modify how your system runs without messing with the next person's config.

To really be able to work with mod_rewrite you would need a basic understanding of Regular Expression, you can read more on it here: http://en.wikipedia.org/wiki/Regular_expression.

Now on to some basic code, this will allow you to create a very robust site with multiple urls strings without having to ever change your .htaccess file/mod_rewrite configurations.

RewriteEngine on

# Rewrite URLs of the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I stole the .htaccess code from the Drupal core. So thank you Drupal, very useful code.

Right, so u paste that into your .htaccess file and upload it to your server. Next you need to create the index file, this will run our little application and will also handle all of the navigation.

create an index.php file in the same directory as you put your .htaccess file, if you dont do this the system will not work very well :)

put this code inside the index.php file:

Home 
A link

<?php // this is just a little demo to show you how it works, // its really not much more that a Hello World app :) // declare the GET query. $q = $_GET['1']; // sanitize the value a little. (you should so a little more.) $q = trim($q); // write a quick switch statement to handle the nav. switch ($q) { // as this is a demo we will make static values. // you can just as easily make dynamic vars as well. case "": echo "there is no link selected"; break; case "home": echo "welcome to my demo app"; break; case "a_link": echo "another link here :)"; break; } ?>

there is a whole lot more that you can do with mod_rewrite, but this is just a simple little demo to help you understand.

Have fun ... :)

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_rewrite 2

hey cool stuff. But to add to that, since you got this article from Drupal.org, i would want to say that using mod_rewrite makes you setup your drupal links to have clean urls. which goes in a way to help search engines in crawling your site well or making your site search engine friendly.

umm...

@Unkown person umm, i did not get this article from Drupal, i just stole the .htaccess file from Drupal.

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