In Asp.net Mvc 5 How we can redirect another page if controller name is invalid in url like http://localhost:51056/free
Redirecting to a controller name that doesn't exist will always return an HTTP 404 (Not Found) response.
Probably the best way would be to set up a redirection rule on your server that redirects /free to some other path that does exist.
You can set configuration in web.config like
<customErrors mode="On" >
<error statusCode="404" redirect="/404.shtml" />
</customErrors>
or You can do this in Global.asax
if (Context.Response.StatusCode == 404) {
// handle this
}
Related
I want to 301 redirect all old domain pages to a new domain home page.
e.g.
olddomain.com -> newdomain.com
olddomain.com/page -> newdomain.com
If i set in web config:
<httpRedirect enabled="true" destination="http://www.newdomain.com/" httpResponseStatus="Permanent" />
olddomain.com/page redirects to newdomain.com/page.
I need to use exactDestination="true"
In my production environment, I would like to redirect all requests to /trace.axd to return HTTP 404. Currently, the default HTTP 500 is returning. This creates all sorts of unnecessary noise in our analytics tools. Environment is ASP.NET 4.0 web forms on IIS 7.5.
Remove the tracing HTTP handler in the Web.config file:
<system.webServer>
<!-- remove TraceHandler-Integrated - Remove the tracing handlers so that navigating to /trace.axd gives us a
404 Not Found instead of 500 Internal Server Error. -->
<handlers>
<remove name="TraceHandler-Integrated" />
<remove name="TraceHandler-Integrated-4.0" />
</handlers>
</system.webServer>
Navigating to /trace.axd now gives us a 404 Not Found instead of 500 Internal Server Error.
First that comes to my mind is to intercept BeginRequest event in global.asax:
protected void Application_BeginRequest()
{
// assuming that in your production environment debugging is off
if (!HttpContext.Current.IsDebuggingEnabled && Request.RawUrl.Contains("trace.axd"))
{
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.End();
// or alternatively throw HttpException like this:
// throw new HttpException(404, "");
}
}
I am trying to redirect all non existent pages to a different domain, as my blog was moved to a different domain.
So www.mydomain.com/blog/asdf should redirect to blog.mydomain.com/blog/asdf
Using Intelligencia URLRewriter module I can redirect blog/ but if I do blog/something I get a 404.
Even with a simple rule without regex like this one, it doesn't work for anything under the blog folder
<rewrite url="~/blog/^" to="http://blog.softwaresynergy.com/blog/" />
I also tried this to force all requests to go to the handler
<modules runAllManagedModulesForAllRequests="true">
Any ideas on how to pick up everything under blog/ and redirect to the other domain?
Try with following redirect rule:
<redirect url="^/blog/(.+)$" to="http://blog.softwaresynergy.com/blog/$1" />
put it at the top of your other rewrite rules, so it gets executed first, I think it should work.
URL Rewriting does not allow to rewrite path on other domain or subdomain. You can redirect the url by using this code in global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
string path = Request.Path;
if (path.Contains("blog/"))
{
HttpContext.Current.Response.Redirect("http://blog.softwaresynergy.com/blog/");
}
}
Use a custom 404 page.
<system.webServer>
<httpErrors existingResponse="Replace" errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/Custom404.aspx" responseMode="Redirect" />
<customErrors mode="On">
<error statusCode="404" redirect="/custom404.aspx" />
Inside the custom404 page I'd put code to do my redirect. Getting the path that caused the error would probably be a combination of...
Request.QueryString("aspxerrorpath");
Request.UrlReferrer;
Once I have the path they were trying to access just do a redirect.
Response.Redirect(NEW_SITE + PATH, true);
I did the change in a different way. Left the original site as php and did the redirect using php rewrite which works fine.
In the domain new.mydomain I did the aspx site
Not ideal, but worked for now.
I want to convert my 404 status code in to 301. I have top indexed site in SEO and I don't want to loos my position if bot don't found my page.
For some URLs I am getting 404 status and by redirecting to some another page with 404 status code is not good thing for SEO.
So I want to convert status code to 301 so it wont affect my indexing.
I want to set 301 status code when it gives 404. My site is in asp.net 1.1 and I have tried lot of solution before.
Some solutions are giving me 301 instead of 404 status code but they gives in second step. Means first they are giving 302 then they are giving 301. But I don't want to do that. I want directly from 404 to 301.
Is it possible with asp.net 1.1 and IIS?
Please help me in this.
Thanks
You can configure error redirects using your application's web.config. Here's an example:
<customErrors mode="On" defaultRedirect="~/somecatchallpage.aspx?type=error">
<error statusCode="404" redirect="somecatchallpage.aspx?type=404"/>
<error statusCode="301" redirect="somecatchallpage.aspx?type=301"/>
</customErrors>
Here's some additional reading (for asp.net version 1.1): customErrors Element
Did you try it with the global.asax?
void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
if (exception is HttpException)
{
Response.Redirect(..);
}
}
How should I configure the URL Rewrite Rule in IIS7 to redirect my aditional domains ( domain.net, domain.org, domain.info) to the principal .com domain?
In IIS7, you can use the new command “appcmd.exe” to enable redirection as following:
%windir%\system32\inetsrv\appcmd set config "Default Web Site/" -section:system.webServer/httpRedirect -enabled:true -destination:"http://domain.com"
This tells IIS to redirect all request sending to the virtual application “/” to “http://domain.com”. The actual result is that appcmd.exe adds the following section to the web.config file for “/”:
web.config of your domain.net
<system.webServer>
<httpRedirect enabled="true" destination="http://domain.com" httpResponseStatus="Permanent"/>
</system.webServer>