Solved

mod_rewrite problems. 404 problems for images...

Posted on 2006-07-12
6
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:slyong
ID: 17096670
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
ID: 17113228
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
ID: 17115910
RewriteRule ^(writings/.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png)$
RewriteRule ^writings/(.*)$ /display.php?article=$1 [L]
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

Author Comment

by:sinisterfrog
ID: 17117203
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
ID: 17118457
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
ID: 17118620
great :)
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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