Jump to content

Simple .htaccess Mod Rewrite guide


yzerman

Recommended Posts

After looking over various sites, trying to quickly grasp the concepts behind mod rewrite - and finish my project by the 7pm deadline, I finally found out exactly how to do what I was trying to do and I figured I would share with the rest of the class.

 

Ok the goal behind this mod rewrite was to enable the website to use a url like http://www.foo.com/bar to pull the file bar.php. After numerous failed attempts at getting this to work correctly, I finally went to the apache site, and tried a few of their examples. The one I found that worked was:

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}  !-s
RewriteRule ^(.*)$          $1.php

 

This however led to another problem. When I went to http://www.foo.com/ without a directory, Apache didn't see anything to rewrite and threw an Internal Server Error fit, so I had to go back and try and figure out this new problem. Now I suck at mod rewrites, and I know little to nothing about Apache configuration - but I dived deep into the Apache mod_rewrite documentation, and I'll be damned if the first thing I tried actually worked:

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}  -d
RewriteRule ^(.*)$          index.php
RewriteCond %{REQUEST_FILENAME}  !-s
RewriteRule ^(.*)$          $1.php

 

Now you have a simple .htaccess mod rewrite guide, and I have something to come back to when I have to do this again. I know this is probably second nature to most of you guru's out there, but for someone who has no idea what to do, this should help out tremendously.

 

Now let me explain this line by line so that the ones who have no idea what the hell this is - can figure it out:

 

First line:

RewriteEngine On

 

This one is pretty self explanatory, this turns the Rewrite engine on if it is not already.

 

Second Line:

RewriteCond %{REQUEST_FILENAME}  -d

 

This is a rewrite conditional (RewriteCond) statement that checks to see if the filename (%{REQUEST_FILENAME}) is a directory (-d). If this conditional turns out to be true, it uses the Rewrite Rule on the third line, if not, it skips to the next rewrite conditional.

 

Third line:

RewriteRule ^(.*)$          index.php

 

This line only comes into effect if the conditional in the second line is TRUE and the file name is actually a directory. What this does, is it tells apache to pull index.php. You can probably change this to whatever file you want apache to pull as the default index file.

 

Ok, on to line 4. This line only comes into play if line 2 turns out to be FALSE.

 

RewriteCond %{REQUEST_FILENAME}  !-s

 

This line, like the first conditional in line 2, checks to see if the file name (%{REQUEST_FILENAME}) is a file with a size (-s). If this conditional turns out to be true, it uses the Rewrite Rule on the fifth line, if not, then it will throw up an internal server error. Which means I should probably add another conditional, but for my purposes, this works.

 

5th Line:

RewriteRule ^(.*)$          $1.php

 

This line matches the file name (in this example lets use bar) and tells the Apache server that what we really want is bar.php.

 

 

Hopefully this helps out getting you started with mod rewrite.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.