Error 400 Bad Request - individual redirect - asp.net

The Seo Company ask for making redirect each 404 bad request to specific url.
I asked for redirect an error 400 (Bad request) to special page.
I can do it in the web.config file, but the requirement is each url to special page
for example
https://stackoverflow.com/questions/askhttps://stackoverflow.com/questions/ask =>(301) https://stackoverflow.com/questions/ask
https://stackoverflow.com/questionshttps://stackoverflow.com/questions => (301) https://stackoverflow.com/questions
Is it possible?
My application is ASP.NET (Not MVC) on IIS SERVER
Thanks

Caveat:
You say this is "ASP.NET (not MVC)". I'm going to assume this means you're using WebForms rather than ASP.NET Core. The following advice can be used in both cases, but in case of ASP.NET Core only if you're hosting with IIS.
Answer:
You're looking for IIS URL Rewrite, a module that can be installed in IIS then configured via your web.config. One feature this provides is rewrite maps which are essentially a list of from and to URL mappings where a request for the former will cause a redirect to the latter. The documentation for URL Rewrite maps is here, and here's an example from the documentation:
Define your mappings:
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRewrites" defaultValue="">
<add key="/article1" value="/article.aspx?id=1&title=some-title" />
<add key="/some-title" value="/article.aspx?id=1&title=some-title" />
<add key="/post/some-title.html" value="/article.aspx?id=1&title=some-title" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Then add a rewrite rule to use the mappings you just defined:
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
In your case you'll want to use the Redirect action rather than Rewrite.
The module provides a lot of other configuration options.

On asp.net webforms set a page i.e. Redirect.aspx to redirect all the Error of type 400:
<configuration>
<system.web>
<customErrors defaultRedirect="Error.htm"
mode="RemoteOnly">
<error statusCode="400"
redirect="Redirect.aspx"/>
</customErrors>
</system.web>
then on the page see the Url it cames from by:
Request.UrlReferrer
then handle accordingly

Related

ASP.NET rewrite redirect url on IIS (for Facebook social login)

I have an application that is behind a reverse proxy and when the user is trying to log in with a Facebook account, it fails because the redirect uri is an internal uri, instead of the public domain.
To come around this I want to rewrite redirected url.
This is the flow:
User logs in to Facebook
Microsoft.AspNetCore.Authentication.Facebook nuget package takes over the authentication process
A redirection (302) GET request happens to this url:
https://www.facebook.com/v2.6/dialog/oauth?client_id={clientid}&scope=public_profile,email&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%3A5000%2Fsignin-facebook&state={longstring}
And this is the part that I want to rewrite:
redirect_uri=https%3A%2F%2Flocalhost%3A5000%2Fsignin-facebook to
redirect_uri=https://mypublicdomain.com/signin-facebook
Is this possible with IIS Url rewrite module?
I tried to configure it but couldn't get it to work.
(on the server I'm using ASP.NETCORE 1.1.2)
<outboundRules>
<remove name="Rewrite Location Header" />
<rule name="Rewrite Location Header" preCondition="IsRedirection" enabled="true">
<match serverVariable="RESPONSE_Location" pattern="^https:\/\/(.*&redirect_uri=)([^&]*)(.*)" />
<action type="Rewrite" value="{R:1}https://google.com{R:3}" replace="false" />
</rule>
<preConditions>
<preCondition name="IsRedirection">
<add input="{RESPONSE_STATUS}" pattern="3\d\d" />
</preCondition>
</preConditions>
</outboundRules>
Yes this is possible. Make sure to use the {QUERY_STRING} input to match against and ensure you have Rewrite as the action type.
A similar solution you could use as a starting point can be found here

asp.net core 2 multiple web.config files (different environments)

This issue is not related to application configurations (custom), but more to do with IIS settings.
So I need the following to be in the web.config when i create a publish for my app.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>
However, when debugging i only want the part and not the http redirect (If i try to debug my app with the rewrite in the web.config it does not start)
in previous asp.net, we could have multiple web.configs for debug and release and it would transform when published.
I simply want to the all of the above code to be in the web.config when published, and only part to be in applied in web.config when i am debugging
This isn't a true answer to your question, but I've got what I think is a much better solution overall. For some time, I've found the fact that URL Rewrites have to go into the Web.config to be frustrating. As careful as you are, it's almost inevitable that you're going to overwrite the Web.config at some point, removing rewrites that have been added to it. This is especially the case if a developer doesn't know better and adds a rewrite directly through IIS, but never copies it over to the project's Web.config in source control (which happens more often than not).
As a result, I started creating a site in IIS just for redirects like this. It has nothing but a Web.config, and then I add the bindings that I'm redirecting from to it. For example, for a rewrite like this, you'd add the binding for the HTTP version of your domain to the redirect site and the HTTPS binding to the actual web application site. Then, you can create the rewrite rule on the the redirect "site", and never ever worry about accidentally overwriting it, because you never publish anything there. This would effectively side-step your issue here, entirely.

How to stop redirecting to other domain by configuring IIS or web.config?

In modern CMS, there are a number of places that redirect users by using returnUrl querystring. For example, redirect user to an internal Url after a successful login.
The problem is that the returnUrl is modifiable by anyone and is hence vulnerable. One way to handle this is to validate the parameters of the application script/program before sending 302 HTTP code (redirect) to the client browser. However, this requires changing of application code.
How can I handle it in IIS level? Is it possible to show an error page if the user is redirected to other domain without touching the application code?
I figured it out.
Install IIS URL Rewrite Module and then edit web.config of the web application and add the following in system.webServer node:
<rewrite>
<outboundRules>
<rule name="Rewrite Location Header" preCondition="IsRedirection" enabled="true">
<match serverVariable="RESPONSE_Location" pattern="http[s]{0,1}://localhost/(.*)" negate="true" />
<conditions>
</conditions>
<action type="Rewrite" value="http://{HTTP_HOST}/error.html" replace="true" />
</rule>
<preConditions>
<preCondition name="IsRedirection">
<add input="{RESPONSE_STATUS}" pattern="3\d\d" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>

Blocking invalid URL and redirect to homepage in IIS

Currently I am using the web hosting service provided by another company. It uses IIS 7/IIS 8 and allows customers to modify web.config according to their needs.
Suppose I have set up two valid URL:
http://www.example.com
http://dev.example.com
Now, when I try to access http://abc.example.com, HTTP Error 403.14 - Forbidden is returned; and if I then try to access http://www.example.com/abc, HTTP Error 404.0 - Not Found is returned.
How can I configure web.config such that users will be redirected to http://www.example.com when they are trying to access an invalid URL?
I have tried to use the snippet provided by the company but without success:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In system.web in webconfig tag add this tag
<customErrors mode="On" defaultRedirect="~/Home/Index">
</customErrors>
This will redirect you to the home page whenever an error occured
You can be more specific with redirection by adding in customErrors tag
<error statusCode="401" redirect="/error/otherpage"/>
This will indicate the page that you will be redirected to when 401 error occurs

Permanent 301 redirect for ASP.NET IIS7

Im trying to figure out how to setup a 301 permanent redirect for a website that is placed on a Microsoft-IIS/7.0 type server.
So let's say I have domain www.A.com and I want to redirect this to www.B.com I could use something like the following in my web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url="A.com" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.B.com$" />
</conditions>
<action type="Redirect" url="http://www.B.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When placing the web.config in the root directory, the server responds:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
Any suggestion why this 403 error is given?
Thanks in advance.
If you are getting a 403 error with a plain (default) web.config and totally vanilla Default.aspx, then there is a configuration problem with IIS. Most likely the app pool does not have rights to the base folder for the website. Sounds like you're in a hosted situation so contact the system administrator.
This should work and also be much simpler. You don't need URL rewriting, just HTTP redirect.
As for the 403, does the IIS application pool have read access to the folder at the root of your site?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="www.B.com" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>

Resources