Solved

mod_rewrite problems. 404 problems for images...

Posted on 2006-07-12
6
241 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

Suggested Solutions

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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