Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

RewriteRule rethink question

rewriterule everything except

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

Junior Member

5+ Year Member

joined:Mar 4, 2010
posts: 134
votes: 0

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.
2:40 pm on Mar 12, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 4, 2010
posts: 134
votes: 0

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]
9:51 pm on Mar 12, 2012 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
votes: 244

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 ;)

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.
9:58 pm on Mar 12, 2012 (gmt 0)

Senior Member

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

joined:July 3, 2002
votes: 0

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".