What is wrong with this rewrite rule - redirect

I am attempting to create a redirect rule to direct all traffic to one of my pages on my old site to my new site. I have created this rules in the parent web.config on my old IIS server:
<rule name="redirect to survey domain" stopProcessing="true">
<match url="takesurvey" />
<conditions>
<add input="{HTTPS}" pattern="^emory.zanek.net$" />
</conditions>
<action type="Redirect" url="https://survey.zanek.net/{R:1}" redirectType="Temporary" />
</rule>
Essentially I want to redirect all traffic for the specific page takesurvey to a new survey domain. So if the user navigates to:
https://emory.zanek.net/goalw4/takesurvey.aspx?id=fasidisdf
it should redirect to:
https://survey.zanek.net?id=fasidisdf
However, When I applied the rule, and then I navigate to https://emory.zanek.net/goalw4/takesurvey.aspx?id=fasidisdf, it just hangs forever. What am I missing here?

You are specifying {HTTP_HOST} but both of your URLs are HTTPS which operates on a different TCP protocol.
For your simple scenario I would just change {HTTP_HOST} to {HTTPS} in your input line.
For more information about this and robust ways to implement more complex/multiple cross types, see the link below.
Web config rewrite HTTPS

Related

Restrict requests by HTTP referer - IIS

I have an IIS website at mydomain.com/mywebsite which has a URL rewrite request that for any requests to a 'maps' path (mydomain.com/mywebsite/maps) through the website, it will redirect requests to an external site to retrieve mapping image tiles that display on the page.
If someone tries to go to mydomain.com/mywebsite/maps on their browser, I do not want it to be accessible as I want to restrict these requests only to the website.
How can I set up a restrict rule in IIS to configure this?
You can try to create a request blocking rule for this, here a example for you as reference:
<rule name="RequestBlockingRule1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^maps$" />
</conditions>
<action type="AbortRequest" />
</rule>
I have managed to restrict access to the URL path by adding an HTTP_REFERER condition in my URL inbound rewrite rule so it only accepts requests from my website domain which seems to work.
<conditions>
<add input="{HTTP_REFERER}" pattern="*mywebsite.com*" />
</conditions>

IIS 10 URL redirect from one domain to another

I would like to redirect a specific site from one domain to another and i have tried the following URL redirect. Not sure where i am going wrong.
I have an app at http://www.a.b.com/myapp/index.aspx
I would like to redirect it to
http://www.a.c.com/myapp/index.aspx
<rule name="testredirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^myapp/index.aspx$" />
<action type="Redirect" url="http://www.a.c.com/{R:0}" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.a.b.com$" />
</conditions>
</rule>
The rule work perfectly on my side. Please try to clean browser cache and post your detailed error message. Please ensure you are running the rule in the right place and has been enabled.
Besides, please check whether the incoming request is reaching the correct application pool.

In IIS 8.5, how do I map a sub-domain to an application within a site?

Currently I have an IIS 8.5 server with a URL of:
subdomain.domain.com/application_instance_name_1
Under a site called Portal Application there are 3 instances of the application, one for each customer.
I would like to create a subdomain per customer and direct it to their version of the application under the Site called Portal Application, for example, Customer 1 would type in:
customer_1_name.domain.com
and in IIS it would be directed to the site and Application below:
Sites/Portal Application/customer_1_application
This is my first time posting on here, I did find something that maybe relevant here but its for Apache not IIS.
IF you need further information please let me know.
Thanks
brian
1) You need to install URL Rewrite and ARR module for IIS
2) Enable ARR. On the Application Request Routing page, select Enable proxy
3) Create rewrite rule
<rewrite>
<rules>
<rule name="rewrite customer_1_name.domain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^customer_1_name.domain.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://subdomain.domain.com/Portal Application/customer_1_application/{R:1}" />
</rule>
</rules>
</rewrite>
P.S. You might have problem with your resources(images,styles,css,js). Because your html might contains absolute paths to resources
You can check this post, when author is creating outbound rule for fixing relative urls https://blogs.iis.net/carlosag/setting-up-a-reverse-proxy-using-iis-url-rewrite-and-arr

Need to get a site redirecting both to www and https

Parts of other answers on Stack Overflow worked, other parts didn't, so I thought I would ask for the full solution including IIS settings re bindings.
I have a site (old ASP classic) that is being moved to IIS 8 (2012).
The site works when testing it from my hosts file.
At the moment I can only get it to redirect http to https not non www requests to www.example.com as well.
So I have 2 bindings in IIS (80/433) for www.example.com, I also tried it with another set without the www. e.g example.com but then I got errors such as the system is trying to process your request in such a way it won't return e.g following redirects around in a circle etc.
I am not so sure if that was about the 2 lots of bindings or the rules it was getting a bit messy then. So I need the whole approach from IIS binding setup and web.config rules (or is there a better way in IIS 8 to force all 80 traffic to port 443?)
I already have a number of ISAPI rules in my web.config file such as
<rule name="Order" stopProcessing="false">
<match url="^applications/order/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="plugins/order.asp" />
</rule>
<rule name="Robots 1" stopProcessing="false">
<match url="^robotstxt\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="robottxt.asp" />
</rule>
<rule name="Robots 2" stopProcessing="false">
<match url="^robotstxt$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="robottxt.asp" />
</rule>
So when I read up I saw about people trying to to do both rules at once or in two sets but it just never managed to work foe me.
I need non http requests to go to https
and for non www requests to go to www.example.com e.g https://www.example.com
At the moment it's partially working but only because I am not trying the non www. redirects.
I have 2 bindings for the www.example.com hostnames and this one rule at the top.
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
So all the rules are working including the http to https. However I need non www rules to work as well.
To get it to keep the rest of my rules AND to get it to ALSO redirect non www. traffic to www.example.com e.g https://www.example.com. What is the best way to do this.
Do I need another pair of bindings set up without www. e.s example.com for 80/443?
Then, what rules do I add in and what order do they need to be as int .htacces they are processed top to bottom in loops an I just wonder with IIS way there is also a logical path that could trip it over causing infinite loops.
I think adding the include is what I would do if you needed to get on top of this very quickly. Use a search and replace program to add the include tag into all of your pages:
<!--#include file="redirect.asp"-->
Then create the "redirect.asp" with something similar to below:
<%
webDomain=LCase(Request.ServerVariables("HTTP_HOST"))
scriptname=Request.ServerVariables("SCRIPT_NAME")
If InStr(webDomain,"https://www.example.com")=0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "https://www.example.com" & scriptname
End If
%>
You may need to tweak that a bit but its the basics of a coded redirect anyway.

Advanced ASP.NET MVC routing scenario

I have an ASP.NET MVC app with the following deployment requirements:
The URL structure must be something like:
http://server/app/[enterprise]/[communinty]/{controller}/{action}/...
What I think I want to be able to do is intercept the URL before the MVC route handler gets its hands on it, remove the [enterprise]/[community] parts, and then allow MVC to continue processing as if the original URL had not contained those two segments.
Here's why:
The application exposes multiple portals to multiple customers (enterprises), and each community within an enterprise has its own user population. This kind of scheme could also be served by physically deploying one application instance (binaries,content,web.config) into each [community] directory, but for logistical and performance reasons, I don't think we want to go down this path. So I'm trying to virtualize it through routing tricks.
Any suggestions on how to go about this scheme, or alternate solutions would be appreciated.
We are on IIS 7, if that makes any difference.
You can use the following route before the default route
routes.MapRoute(
null,
"{enterprise}/{community}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can then ignore {enterprise} and {community} parameters in your action methods.
Here is a possible solution with IIS Rewrite module. It may not be the best approach, but it may work. Is there an easier/better option within the MVC routing? Not sure. Only just started doing that myself.
Using "http://server.com/app/enterprise/community/controller/action/" as an example.
What happens:
Strips the string out of the URL. New
URL:
http://server.com/controller/action/
Redirects the user to new URL. User's
browser now shows:
http://server.com/controller/action/
Takes the new URL and tries to
rebuild it to grab the correct
content. User's browser shows:
http://server.com/controller/action/ ;
IIS returns:
http://server.com/app/enterprise/community/controller/action/
All of this would be in the web.config once the IIS rewrite module is installed:
<rewrite>
<rules>
<clear />
<rule name="Redirect to remove Offending String" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="server.com/app/enterprise/community*" />
<action type="Redirect" url="/{R:1}" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_NAME}" pattern="*server.com*" />
</conditions>
</rule>
<rule name="Rewrite to get Original Content" enabled="true" patternSyntax="Wildcard" stopProcessing="false">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_NAME}" pattern="*server.com*" />
</conditions>
<action type="Rewrite" url="app/enterprise/community{R:1}" />
</rule>
</rules>
</rewrite>
Note: Just did this quick, haven't tested.