Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My mod_rewrite guide
#1

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.

Reply
#2

Everything went fine however i did run into one snag. when i tried to test it out it didnt seem to be reading the htaccess file so nothing happened. I tried alot of differant stuff like restarting httpd and checking for typos. What I had to do to make it work was edit my httpd.conf file. So if anyone tries this and it doesn't work, try adding the following to httpd.conf ....

 



Code:
#added by tek begin
#
<Directory /var/www/html/imagestek/>
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>
#




 

cut an paste it right into httpd.conf around line 390, right after it say AccessFileName .htaccess

next change the name in the comment to your own :P

then adjust the directory path to suit your needs.

after i did that i restarted httpd and it worked just like hijinx said it would.

 

thanks for the info hijinx

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)