Welcome to WebmasterWorld Guest from 54.221.87.97

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

RewriteRule rethink question

rewriterule everything except

     

hottrout

11:32 am on Mar 12, 2012 (gmt 0)

5+ Year Member



I currently use the following rule to rewrite all requestes for any htm or html pages in a given url to the new php script. It works fine however I want to change the rule so that instead of just redirecting html or htm pages, it would redirect everything except images, ie .jpg, .gif, .bmp, .png.

Can you help me make this expression work this way round and also make it as efficient as possible?


RewriteRule ^Libraries/Emulation/NES/(([^/]+/)*)([^/.]+\.html?)$ http://www.example.com/roms/index.php?folder=Nintendo/NES [R=301,NC,L]


Thanks in advance.

hottrout

2:40 pm on Mar 12, 2012 (gmt 0)

5+ Year Member



Sorry to double post but would this be the answer. I have not tested it yet but wanted to try and solve this myself.

RewriteRule ^Libraries/Emulation/NES/(([^/]+/)*)([^/.]+\.!(jpe?g|gif|bmp|png))$ http://www.example.com/roms/index.php?folder=Nintendo/NES [R=301,NC,L]

lucy24

9:51 pm on Mar 12, 2012 (gmt 0)

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



I'd be interested to see the results, but I'm pretty sure you can't use ! in the middle of a pattern. In vanilla Regular Expressions it's only used in lookaheads/ lookbehinds; its broader "not this" meaning is specific to mod_rewrite. Also some other Apache mods, but never mind those.

Picking apart the pattern as long as I'm here ;)
^Libraries/Emulation/NES/(([^/]+/)*)([^/.]+\.!(jpe?g|gif|bmp|png))$


begins with literal text Libraries/Emulation/NES/

(([^/]+/)*) = there may or may not be additional directories here. Since you're ultimately looking for something with an extension, you can shave a couple of nanoseconds by expressing the inner bit as [^/.]+/

[^/.]+ = a final bit of plain text excluding directory slashes and periods

\. = literal period

So far it looks really good. It's just that mid-pattern ! that I'm leery about.

:: detour here ::

OK, I went and tested it in my art studio's site which never gets any traffic. I'd actually expected a 500-class error, but all that happened was that the rule didn't execute. I deleted the ! and it worked as intended: that is, it redirected a jpg request to the nonexistent page I'd specified.

But there's got to be a finite number of possible extensions on your "real" files, especially if you've already constrained the search to a particular sub-sub-directory. For example, you don't want to redirect css files do you? (Was going to say robots.txt, but of course it doesn't live in a triple nest of folders ;) )

So the rule will work nicely if you replace the !(jpe?g) et cetera with the desired extensions, say (jsp|php|html?) et cetera.

g1smd

9:58 pm on Mar 12, 2012 (gmt 0)

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



Using "not" can work in some circumstances, but when there's a match the backreference will always be empty. It's not the best way to do it.

Use your original RewriteRule but replace the \.html part with .* or similar. Add a preceding RewriteCond that looks at REQUEST URI and make that pattern match "not an image".