Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod rewrite, ignore escaped characters for directory

10:03 am on Jan 24, 2014 (gmt 0)

New User

joined:Jan 24, 2014
votes: 0


I've created a caching function which caches images and then uses urlencode() (PHP) on them and put them in 1 cache directory, and thus a cached image file name can look like this: 400%2F0%2Ffiles%2Fimage.jpg but my htaccess thinks the %2F are slashes and returns a 404 because of it.

What I'd like is for the htaccess to ignore escaped characters in the specific folder 'cache/' =P

I've exhausted all of my ideas, can anyone help? :)

Here's my current htaccess:

Options -indexes

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*)$ %{REQUEST_URI} [R=404,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.php/$1 [L,QSA]

RewriteCond %{REQUEST_URI} ^/core/.*
RewriteRule (.*)$ index.php/$1 [L,QSA]

-- Thareh
10:24 pm on Jan 24, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 536

:: detour to Apache docs followed by prolonged experimentation in test site ::

It can be done, but it's so convoluted and peril-fraught that I honestly think you're better off with a simple, conditionless

RewriteRule ^cache/(.+) /fixup.php?path=$1 [L]

and let the php do the converting. The alternative is to make a series of RewriteRules-- as many as your directory structure is deep-- on this pattern:

RewriteRule ^cache/([^/]+)/([^/]+)$ /cache/$1%%22F$2 [L,NE]

Did you get the %%22? You have to do this because "%2" alone is interpreted as "the second capture from the most recently met condition" even if there never was a condition. And then you need the NE flag to keep it from turning into something with %25 in the middle representing a literal percent sign.

A final quirk is that when I did this experimentally, I ended up with a double-404: first the expected 404 because I was redirecting to nonexistent pages, and then a further error message that said it couldn't find the 404 page. I suspect this is because the [NE] flag remains in effect somehow, so the server is looking for "boilerplate%2Fthe404page.html". (The last bit is only a guess. I'd have to do some further experimenting in MAMP with a RewriteLog to make sure.)

Well. That was interesting :)
7:21 am on Jan 27, 2014 (gmt 0)

New User

joined:Jan 24, 2014
votes: 0


Thank you very much for your experiment, very nice of you to try and solve this annoying dilemma =P

Seems the best way to go is to use PHP or just replace all the forward slash characters with something else that apache doesn't convert automatically then =P

Thanks again,
-- Thareh

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members