Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mod_rewrite problems. 404 problems for images...

Posted on 2006-07-12
6
Medium Priority
?
245 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 1332 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
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 …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

618 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