response.Addheader not working anymore - iis-7

i have a page for 404 error and the response.status is 404
yesterday the response.Addheader was working fine but today it is just showing a blank page and if i check the header status it is 404.
here is part of my code
Response.Status="404 Not Found"
Response.AddHeader "Location", domain & "/page-not-found"
in the same code - the 301 status works fine
IIS 7, vb script, classic asp

You could edit your web.config to redirect to there on 404...
<configuration>
<system.web>
<customErrors defaultRedirect="/page-not-found"
mode="RemoteOnly">
<error statusCode="404"
redirect="/page-not-found"/>
</customErrors>
</system.web>
</configuration>
Source:Web.config custom errors

Related

Forceful 503 redirect doesn't show custom page

I'm implementing google's best practice to get 503 status code when my site is under maintenance.
I do it by checking a value in web.config and then the following code for redirecting the flow to my custom page
Response.Redirect("/maintenance", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Inside Page_Load() of the 'maintenance' page I've got
Response.StatusCode = 503;
Response.StatusDescription = "HTTP/1.1 503 Service Temporarily Unavailable";
Response.AddHeader("Retry-After", "Sat, 30 Mar 2016 23:00:00 GMT");
The strange thing happening here is that running the above code shows me the error page of IIS rather than my custom page. Also the same code works fine in my other application.
If I don't assign to Response.StatusCode, it works fine.
I've got an entry in my web.config for a 404 redirect like so
<httpErrors errorMode="Detailed" existingResponse="Replace" defaultResponseMode="Redirect">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/error-page" responseMode="ExecuteURL" />
</httpErrors>
This makes sense. But I doubt it as commenting it redirects me to my custom page.
Any help is appreciated.

DefaultRedirect not working on mvc 4 web.config

Hi there fellow programmers! I have a problem, I am facing a fancy problem of redirecting to a custom error page. Here is what i have in my web.config
<system.web>
<customErrors mode="On" defaultRedirect="/Message/DefaultError" redirectMode="ResponseRedirect" >
<error statusCode="404" redirect="/NotFound" />
<error statusCode="500" redirect="/ServerError"/>
</customErrors>
</system.web>
on error 404 and 500 which were declared above were working fine, but my main problem is working with not enumerated errors such ass 400 etc.. i want to catch ALL OTHER ERROR on the default redirect action. or is that possible??? Help please????
i want to catch ALL OTHER ERROR on the default redirect action
Since 'All other errors' (400, 401 etc) will most likely be considered a Server-Error and not an Application-Error, you need to set the default page on the <system.webServer> section:
<system.webServer>
<httpErrors defaultPath="/Message/DefaultError" defaultResponseMode="Redirect">
<clear />
</httpErrors>
</system.webServer>
See Documentation

invalid or unknown .aspx pages do not trigger 404, and instead display marker file text

On my web server, if i visit a page that does not exist, it generates a proper 404, UNLESS that page ends in .aspx. If the invalid url ends with .aspx, IIS generates the marker file error:
"This is a marker file generated by the precompilation tool, and should not be deleted!"
instead of a proper 404. How can i make IIS display/serve a 404 for unknown or invalid .aspx pages?
For example:
http://www.mysite.com/iDontExist.txt --> generates proper 404
http://www.mysite.com/iDontExist.aspx --> marker file error instead of 404
any ideas?
You need to turn on the custome errors via the web.config
<configuration>
<system.web>
<customErrors defaultRedirect="GenericError.html" mode="On">
<error statusCode="404" redirect="PageNotFound.html"/>
</customErrors>
</system.web>
</configuration>
More information on this can be found in MSDN
The trick was to set the "redirect mode" of the customErrors tag to 'ResponseRedirect' instead of 'ResponseRewrite':
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="customError.aspx" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="customError404.aspx" />
<error statusCode="500" redirect="customError500.aspx" />
</customErrors>
</system.web>

non existing url path throws standard IIS 404 page [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
iis 7.5 hijacking 404
I'm running a .net 4.0 site over a IIS 7.5.
I have added the custom errors section to my webconfig:
<system.web>
<customErrors defaultRedirect="/error" mode="On">
<error statusCode="404" redirect="/error?code=404"/>
<error statusCode="500" redirect="/error?code=500"/>
</customErrors>
</system.web>
The custom errors are working. Any internal server errors are redirected to /error?code=500 and any throw new HttpException(404, "Not found"), when a missing parameter is found, are redirected to /error?code=404. But any non existing url path still throws the standard IIS 404 page, not my custom 404.
Any tip?
That happened to me once in IIS 7, the solution was to configure the IIS-Application error pages:
Open IIS
Select your applciaiton
Open Error Pages under the IIS section
In the right panel named Actions, click Edit Feature Settings
To test choose:
Custom Error Pages
Accept the changes
Restart IIS
That did the trick in my case
Edit 1
Remember that the customErrors section works only for requests handled by ASP.Net the static requests like html pages will still redirect to the 404 error page configured in IIS
However, you could override this behavior in your own config file:
For more info
<configuration>
<system.webServer>
<httpErrors existingResponse="Replace" errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="GenericError.aspx" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
You also might need to mark the above section as updatable:
Open the file:
C:\Windows\System32\inetsrv\config\applicationHost.config
And change:
<section name="httpErrors" overrideModeDefault="Deny" />
To:
<section name="httpErrors" overrideModeDefault="Allow" />
You can handle it in Global.asax or in IHttpModule.
Exception currentException = Server.GetLastError();
HttpException httpException = currentException as HttpException;
if (httpException != null && httpException.GetHttpCode() == 404)
{
// redirect
}
Check http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-integrated-pipeline/ for info how to setup wildcard mapping, so all requests are handled by asp.net.

404 error code redirect back to homepage

I have some broken links on my site, and will keep having new ones on regular basis. How do i write a centralized code in web.config or something for 404 then redirect to home page?
I am using aspx, vb.net and IIS7
In the web.config you can have a section as follows -
<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx">
<error statusCode="404" redirect="/" />
</customErrors>

Resources