Apache Mod Rewrite
Like this blog? Consider exploring one of our sponsored banner ads...
One if the cleanest ways to setup a site is to use mod_rewrite in Apache. I am going to show you a simple way to do this using an htaccess file and a little PHP. First step is to make an htaccess file with the following code:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?loc=$1
Now everything that is not a folder or a file will get passed to the index.php file as $_GET['loc']. You could change the index.php to any file you like and the ?loc= to some other variable if you like.
Next you need to groom the url. Do not leave out this step. You are injecting the url into your script… I suggest something solid like the following:
<?php $url = preg_replace('/[^[:alnum:]\-\/]/', '', $_GET['loc']); ?>
So now you have the url into your script safely so you need to break it apart. You are basically going to pass variables here. All you have to do is explode on the forward slash. You can do it simply like this:
<?php $parts = explode('/',$url); $section = $parts[0]; $subsection = $parts[1]; ?>
About this entry
You’re currently reading “Apache Mod Rewrite,” an entry on BRADINO
- Published:
- 3.24.07 / 5pm
- Category:
- Apache
- Tags:
No comments
Jump to comment form | comments rss [?]