Link to home
Start Free TrialLog in
Avatar of sinisterfrog
sinisterfrog

asked on

mod_rewrite problems. 404 problems for images...

Snippet from my htaccess:
Options +FollowSymlinks
#display articles from writings
RewriteRule writings/(.*)/ display.php?article=$1
RewriteRule writings/(.*) display.php?article=$1

Ok, i'm using mod_rewrite to make URLs prettier on my website http://sinisterfrog.com
I am doing this in the writings section to change something like http://sinisterfrog.com/display.php?article=gamelan to http://sinisterfrog.com/writings/gamelan
That all works fine.  At the moment however I have my site set up with a directory called writings as well (which is where all the articles are stored)
display.php calls the article from the URL, talks to mysql and spits out the appropriate titles etc, then includes the contents of one of the text files in the writings directory.

So, my problem is, there is a better way to do this.... and my bigger problem (the one the points are for) is that images don't work.  If i store an image in the writings directory, I can't access it by going to http://sinisterfrog.com/writings/image.gif because of my primitive rewriterule.

So if you can help me jazz up my rewrite rule a lot, you win, and i'll be very grateful.
Avatar of slyong
slyong

Hi, try this:

RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [L]
RewriteRule ^writings/(.*)$ writings/$1/ [R]
RewriteRule ^writings/(.*)/$ display.php?article=$1

The first line stop the rewrite for jpe, jpg, gif, bmp, png.  The second line takes care of the case if someone type http://sinisterfrog.com/writings/gamelan it will be changed to http://sinisterfrog.com/writings/gamelan/ and then the third line will kick in to make http://sinisterfrog.com/writings/gamelan/ into http://sinisterfrog.com/display.php?article=gamelan (which is what the real thing).
Avatar of sinisterfrog

ASKER

that doesnt seem to work so well....
with the ^'s it rewrites the url to include my home/public_html directory in the URL
without them, i get a 302 found error...

interestingly though, if i keep the rules i wrote, and add the image rule above, then http://sinisterfrog.com/writings/gamelan gives me the result i want
however, add a trailing slash, and the images stop coming up

any one else got any bright ideas?
should i keep it similar to how it is but remove trailing slashes?
ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that seems to do the trick...i think i've got my answer
can you please explain what each line is doing?
Yes, of course. The first rule strips the trailing slash by capturing everything left to it in $1 and issuing a 301 redirect (moved permanently) to the URL without the trailing slash.

The 2nd rule matches only, if the filepath starts with writings/ and captures everything right to it in a back reference $1. In a 2nd step (rule is being processed before conditions) REQUEST_URIs ending with those extensions are being excluded. This condition is only necessary, if you have such files in a folder named /writings/.
great :)
thanks