Forum Moderators: phranque

Message Too Old, No Replies

Execute RewriteRule only if entry exists in RewriteMap file

         

jameshopkins

11:06 am on Jun 1, 2009 (gmt 0)

10+ Year Member



I am wanting to perform an external redirect *then* internal rewrite based on entries within a RewriteMap file. This post builds on the code and context that I mention in my last posts - [webmasterworld.com...]

In case you haven't visited my last post, I'm using the following code,

RewriteCond %{THE_REQUEST} ^GET\ /test\.asp\?meta=([a-z-]+)\ HTTP
RewriteRule ^test\.asp$ http://localhost/${level1-1:%1}? [R=301]

RewriteRule ^([a-z-]+)/?$ test.asp?meta=/${level1:$1

, which executes an external rewrite from test.asp?meta=ho to /horses/, then internally rewrites the URLS, so that when /horses/ is requested, the content within the file test.asp?meta=ho is executed.

What I'd now like to do is to throw a 404 on the internal rewrite (it makes sense to me for the 301 to be executed no matter what) if the mapfile entry that is being requested, doesn't exist.

From my initial investigation, I assume the correct directive to use in this instance is RewriteCond; upon looking into the documentaion, it states that RewriteMap expansions can be used, but I can see no more information on it - I assume this is what I need? I am however, unsure as to how to the syntax for such a rule.

Could someone please give me a few pointers?

Regards

James

g1smd

11:45 am on Jun 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need the [L] flag on both rules for a start.

I can't help for the Mapping stuff, but somehow you need a test that the mapping has failed to match.

jameshopkins

12:57 pm on Jun 1, 2009 (gmt 0)

10+ Year Member



Thanks for the reply, g1smd. I forgot to add those flags but I'll do those when I next do an update.

Thinking more about the original issue, I can see something along the lines of


RewriteCond somevariable !-F

Where the RewriteCond pattern does *not* match the variable as a pathname and tests whether or not it exists, and is a regular file.

I'm not even sure if that would be correct, but that's how I think it could work.

I'm still looking for a few pointers though :)

jdMorgan

2:10 pm on Jun 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the RewriteMap documentation. There is an explicitly-defined option for the "no match found" scenario (called "DefaultValue"), and you can simply specify a path that does not exist as that option. So, when no map entry is found, the rule rewrites to a non-existent path, and that in turn invokes 404 handling.

Jim

g1smd

3:10 pm on Jun 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That sounds simple enough. I'd assumed the OP had read those docs and found nothing. Bad assumption. :)

jameshopkins

3:12 pm on Jun 1, 2009 (gmt 0)

10+ Year Member



I'd read them alright, but understanding them is a different thing ;)

g1smd

3:14 pm on Jun 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



They are a tad on the cryptic side aren't they?

I find that re-reading it every few weeks finds some new gem that now makes more sense - and a lot that still might as well be in Ancient Greek.

jameshopkins

3:17 pm on Jun 1, 2009 (gmt 0)

10+ Year Member



Thanks for the help, by the way, guys. Food for thought, at least!

jameshopkins

10:30 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Upon looking at the issue again, the DefaultValue solution won't ever be able to invoke a 404; our URLs don't contain parameters relating to database entries - instead, different partitions of the URL 'bind-together' the requested page. It's an extremely backwards way of working that I've never seen before.

g1smd

10:56 am on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you do is detect the "default value" and rewtite it to an internal path that does not exist.

That action triggers the server to send the default 404 error message.

RewriteRule ^mapfailed$ /this-path-does-not-exist [L]

jameshopkins

11:30 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Thanks for the reply, g1smd. I can certainly see the logic and thinking behind what you propose, but am unsure how I can detect the DefaultValue; I assume I would use a RewriteCond but other than that, I wouldn't have a clue - could you give me a pointer?

jameshopkins

2:35 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



Please ignore my request for help. I've figured this out

g1smd

3:55 pm on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... and the solution is:

:)

jdMorgan

6:21 pm on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no second step... This function is "built in" to RewriteMap calls. The RewriteMap invocation in the original rule simply needs to include the "what filepath or URL to load if no match is found" optional parameter. See the documentation of RewriteMap calls and look for the exact string "DefaultValue" in that documentation.

Jim