Catch 404 pages?

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 92
Joined: 01 Dec 2012, 14:22

Catch 404 pages?

01 Dec 2012, 14:24

Is there any way to catch 404 pages using rewrite and send them to a fixed place? I particularly want to capture the URL that the original request was looking for.

Running ISAPI_Rewrite 3 on IIS7 :?:

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Catch 404 pages?

02 Dec 2012, 20:35

Hello,

Sure, you can capture original requests. But how do you want to do it? Do you want it to be in the querystring of the 404 page?

You may implement [U]-flag and than IIS log would show original requests, not redirected. You may also use some IIS variables to get it.

Regards
Andrew

User avatar
Posts: 92
Joined: 01 Dec 2012, 14:22

Re: Catch 404 pages?

03 Dec 2012, 08:30

Basically, yes- the situation is that I'm working on a site for a client, and he's moving away from one platform to another.

Some of the "pages" need to be served from a dynamic service, but they need to be "static" URLs. To maintain the pagerank value he already has, the URLs need to be the same as the ones he's already using.

The problem is that there is a set of files that have a format like /some-meaningful-text-c12345.html
- where the 12345 bit is unique for each page. The issue is that I can't find a way to distinguish the real static pages from the dynamic static pages, short of just having a big list that says "rewrite it unless it's one of these 50". I was using custom 404 processing and a bit of ASP code to capture when it doesn't find a page, and then producing the page content dynamically. It's not nice, though, and it does create a nasty URL for the generated page.

Essentially, what I want to do is capture the "c12345" bit from the original URL and either serve the page, if it's really static, or generate it on the fly if it isn't.
I've even considered using the 404 process to generate the page, write it out to the server, and then redirect back to the newly-created "static" page, but that's also quite ugly.

It's not the IIS log that concerns me so much as the pagerank of the pseudo-static pages.

I've just been looking at the support pages, and it looks like a rewrite map might be the thing..

Is it possible to invert the test- so that the URL is rewritten if it's *NOT* found in the map?

I.e. the map could list the *real* static pages, and let everything else go to rewrite.
:!:

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Catch 404 pages?

03 Dec 2012, 22:30

That's exactly what I was going to suggest - mapfiles.

Try to play around with this:

Code: Select all
RewriteEngine on
RewriteBase /
RewriteMap mapfile txt:mapfile.txt [NC]

RewriteCond ${mapfile:$1|NOT_FOUND} NOT_FOUND
RewriteRule ^([^.]+)\.html$ /rewrite_something [NC,L]


Notice that there's no '!' in front of "NOT_FOUND". So it would rewrite all those paths that end with .html and are NOT in mapfile. And in your mapfile you should have:

some-meaningful-text-c12345 some-meaningful-text-c12345
some-meaningful-text-c11111 some-meaningful-text-c11111


doesn't matter what you put in the second column, i guess, just put comething.

Regards
Andrew

User avatar
Posts: 92
Joined: 01 Dec 2012, 14:22

Re: Catch 404 pages?

05 Dec 2012, 15:00

I found a simpler solution, from looking at the Apache docs (which are much more detailed than Helicon's, I have to say).

# magic category rules..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*-c([0-9]*)\.html$ mqh/my.asp?cat=mar&service=make-page&magic=$1 [L,NC,QSA]

If there is a file, it will serve it- if not, it will pass the "magic" key number to my backend server (gated through asp)
Simples!

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Catch 404 pages?

05 Dec 2012, 17:11

Can't say I couldn't come up with that. Probably didn't understand you correctly.

Regards
Andrew

User avatar
Posts: 92
Joined: 01 Dec 2012, 14:22

Re: Catch 404 pages?

06 Dec 2012, 12:42

Seems there is a problem with catching 404's with rewrite, though: it gets in *before* IIS applies default document rules.

I had a last rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) mqh/my.asp?cat=mar&service=custom404&oqs=$1 [L,NC,QSA]

- which caught all manner of things, showing a nice "sorry!" page etc.

The problem was, it stopped index.html working! If you just put in an unqualified domain or domain/folder, it went to the 404 page.
I've had to take that out, and go back to ugly things with asp and custom error pages.

Unless there's a solution? There's a flag on RewriteCond that allows you to test if a URL maps to a directory; does that include the web-root?
That presumably would allow the default document to fire.

Ah! I've just discovered the "NS" option - I suspect all my previous rules should have had the NS option applied, for efficiency.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Catch 404 pages?

06 Dec 2012, 13:53

Hello,

NS-flag is more appropriate in Ape. usually you can go without it, by simply using L.

The best way to find out about index.html is to see rewrite.log. Please, provide rewrite.log for the testing request.
Logging issues described in FAQ

Regards
Andrew

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 32 guests