Solved

mod_rewrite problems. 404 problems for images...

Posted on 2006-07-12
6
237 Views
Last Modified: 2010-03-04
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.
0
Comment
Question by:sinisterfrog
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:slyong
Comment Utility
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).
0
 

Author Comment

by:sinisterfrog
Comment Utility
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?
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 333 total points
Comment Utility
RewriteRule ^(writings/.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png)$
RewriteRule ^writings/(.*)$ /display.php?article=$1 [L]
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:sinisterfrog
Comment Utility
that seems to do the trick...i think i've got my answer
can you please explain what each line is doing?
0
 
LVL 27

Expert Comment

by:caterham_www
Comment Utility
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/.
0
 

Author Comment

by:sinisterfrog
Comment Utility
great :)
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now