LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Apache rewrites screwing up images (https://www.linuxquestions.org/questions/linux-server-73/apache-rewrites-screwing-up-images-778925/)

deesto 12-30-2009 01:08 PM

Apache rewrites screwing up images
 
I have an Apache proxy config with a ton of rewrites to other servers, so in order to handle the "trailing slash problem" in one place, I had added this to the beginning of a virtual host file:
Code:

Options +FollowSymlinks
RewriteEngine on
RewriteLogLevel 0
# any URL request missing a trailing slash gets one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(/)$
RewriteRule (.*)$ $1/ [R,NC]

This works great, with the exception of one rewrite (that I know of so far), which is still adding the trailing slash to the end of every URL referred to by a page, including images (e.g., 'image.jpg' is being turned into 'image.jpg/', which doesn't exist). The rule for that one is currently:
Code:

RewriteRule ^/somesite/(.*)$ http://some.server.com/somesite/somelongpath/$1 [R=301,NC,L]
The thing that's really odd to me is that this URL is being rewritten and, I thought, handed over to the next server, so the proxy shouldn't be involved anymore. But for whatever reason, it's still doing the slash rewrites after the initial remote page is loaded.

One thing I should mention is that this config is running on an old Apache version (1.3), so there may be some differences in config from the versions I'm more accustomed to (v2.x+), even though httpd doesn't complain at all on syntax checks or restarts. Is there a cross-version way to do this? Or maybe just a way that works in v1.3?

kbp 12-30-2009 09:42 PM

You should probably convert it to a rewrite condition for the trailing slash rule rather than it being a rewrite rule by itself

cheers

deesto 12-31-2009 01:21 PM

Quote:

Originally Posted by kbp (Post 3809494)
You should probably convert it to a rewrite condition for the trailing slash rule rather than it being a rewrite rule by itself

Thanks kdp ... which rule do you mean: the site-specific one, or the general '/' rewrite? If the '/', isn't that what the '!(/)$' condition is for? If not, please help me understand?

kbp 12-31-2009 05:22 PM

Sorry.. I wasn't reading carefully enough, apache rewrites are really easy to get wrong.

I usually turn on rewrite logging when troubleshooting :

Code:

RewriteEngine On
RewriteLog /var/log/apache/mysite_rewrite.log
RewriteLogLevel 9

If you move them around like so:
Code:

...
RewriteEngine on
RewriteLogLevel 0

# rewrite for somesite
RewriteRule ^/somesite/(.*)$ http://some.server.com/somesite/somelongpath/$1 [R=301,NC,L]

# any URL request missing a trailing slash gets one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(/)$
RewriteRule (.*)$ $1/ [R,NC]

..does this make a difference ?

deesto 01-05-2010 01:35 PM

Thanks kbp, but no, moving the rules doesn't seem to help ... it may be because I have so many other redirects going on, but moving the slash rule isn't working ...

Here's an example log entry for one image file that shows a redirect for the image file ... but I don't see an error in the entry itself, but the actual HTML renders with missing images (which still show references to "image.gif/"):
http://deesto.pastebin.com/m612bb3a8

... and here is the current config; maybe you'll see a mistake that I haven't noticed:
http://deesto.pastebin.com/m30951176

kbp 01-05-2010 03:19 PM

The image listed in the log didn't get modified because it matched one of the previous rules, the 'trailing slash' rule will only affect requests that don't match any of the previous rules. Could you please provide an example url with the broken image links ?

thnx

deesto 01-05-2010 03:41 PM

Thanks kbp ... sent you a PM with some URLs ... let me know if you can see where I'm screwing up?

deesto 01-22-2010 03:58 PM

Just wanted to make a quick mention that although we weren't able to pinpoint the problem with these Apache directives (bloody rewrite rules!), kbp was kind enough to get in touch with me privately and try to analyze the conf files, log data, etc. I really appreciate his help.


All times are GMT -5. The time now is 09:11 AM.