2005-03-05, 01:22 PM
So I got bored and since I'm doing a lot of mod_rewrite work lately, I have decided to do a little mod_rewrite guide. If anyone wants something done I can try to do it.. but for now I will just explain what things do. The syntax of mod_rewrite is almost like sendmail.. but once you understand it you can do some really great things.
So we'll start off with just a common use for mod_rewrite. I always keep all my images in like /var/www/html/images. So this will show a default error image not found image instead of that nasty broken image box with the X in it. So you want to create a .htaccess file in the /var/www/html/images dir. Then place the following lines in it.
Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) /images/404.png
So the first line just tells apache to turn on mod_rewrite.
The second line is a conditional statement. It says move on to the next line if the requested file does not exist. So the condition is the !-f. The -f checks if the request is for a file. So the ! in front of it is a common programming practice and that basically negates what comes after it. So the total line means if the request is for a file that does no exist in the directory
Then the last line says take any input and replace it with /images/404.png.
So if i have this in html
Code:
<img src=/images/bla.jpg>
if there is no bla.jpg in the images dir it will show a 404.png in its place. Don't worry about image format differences. The browser will hande it.
more to come when i get bored.