URL Rewriting using web.config file in .net MVC Application - asp.net-mvc

I want to write a rule in web.config so that I can redirect URL.
Only for this keyword in URL(/resultprocess/). I want to change domain in URL.
My input URL is : http://A.B.C.D:8082/resultprocess/GetMessage (having any type of input data).
I want output URL : http://M.N.O.P:9092/resultprocess/GetMessage (with same input data).
I have tried follownig rules:
<rewrite>
<rules>
<rule name="Rewrite Rule2">
<match url="/resultprocess(.*)" />
<conditions>
<add input="{URL}" pattern="/resultprocess(.*)" />
</conditions>
<action type="Redirect" url="http://M.N.O.P:9092/{R:0}"/>
</rules>
</rewrite>
But it's not working. Please suggest me, what am I doing wrong?

Related

IIS URL redirection - igonre match all for some URLs

I had to change previous URL pattern to something else. Earlier URL pattern was
www.testdomain.com/hotels/"city" and it changed to www.testdomain.com/hotel-"city"
Below is the redirection I have used that for.
<rule name="cityChange" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^hotels/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="www.testdomain.com" />
</conditions>
<action type="Redirect" url="www.testdomain.com/hotel-{R:1}"appendQueryString="false" redirectType="Temporary" />
</rule>
Now I have another issue which is another URL I have used for the site is also redirecting to different one. That URL is,
www.testdomain.com/hotels/'city'/'name'-'street'-'postalcode'
Since I have used a wildcard for /hotels/'city' to /hotel-'city' it changes above URL as
www.testdomain.com/hotel-'city'/'name'-'street'-'postalcode'
as well. But that URL is not valid in the site and I do not want to replace "/" from "-" there.
How can I exclude this URL pattern from that wild card which I have mentioned above?
Thanks in advance.
Can't be sure about that but did you try to add parentheses, like that ?
<match url="^(hotels)/(.*)" />
We did that for ou specific wildcard redirect :
<rule name="modeles-en" enabled="true" stopProcessing="true">
<match url="^(modeles-en)/(.*)" ignoreCase="true" />
<action type="Redirect" url="/models/{R:2}" redirectType="Permanent" />
</rule>
Hope it helps !

Missing www in my url (on cellphone) causing "This connection is not private" ASP.NET MVC

I've just published my website, and I realized missing www in my (BUT ONLY ON CELLPHONE) url causing this issue:
It is interesting fact that if I type url without www on my computer or laptop website is working fine, but if I type url without www on my cellphone, that is message that I'm getting (screenshot above).
So
"https://example.com" // causing issue (because there is no www)
"https://www.example.com" // works fine on my cellphone
I must say that I've tried editing web.config file, and I added this rule which should redirect all non www to www url, so my web.config looks like this:
<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:0}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
But I do not know what it's about, cuz it's causing issue only on cellphones looks like..
So any kind of help would be great!
Thanks
If you are using https://, all your urls must be with https://. If you will use http:// this will cause errors. Additionaly, you have to setup certificate for both https://www.sample.com and https://sample.com
And check your web.config you use http insted of https

IIS URL Rewrite multiple parameters

I am really confused about the URL rewrite interface. I dont understand what i need to do.
I have an url as:
www.example.com/diretory/subdirectory/index.html?param1=1&param2=2&param3=2&param4=7
I want to hide this url in a <a>-href tag, which displays "Example Tag".
When the url is requested it should rewrite it to
www.example.com/program/location/year/vacancie
I allready tried this:
<system.webServer>
<rewrite>
<rules>
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
<action type="Rewrite" url="www.example.com/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
In the URL Rewrite Interface the Test Pattern said it is working and gets:
?param1=1&param2=2&param3=2&param4=7
param1=1
param2=2
param3=2
param4=7
I checked the log url rewrite as well, but in my logs it is not shown.
2017-03-20 16:29:24 192.168.253.146 GET /diretory/subdirectory/index.html param1=1&param2=2&param3=2&param4=7 88 - 192.168.253.146 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/56.0.2924.87+Safari/537.36 - 304 0 0 4
ps: the urls are not working and only for illustrative purpose.
The match URL only matches the URL and does not take the querystring into account. You will need to add a condition for this. Also do you want to rewrite this (so the server internally executes the new URL) or redirect (so the server will request the browser to go to the new URL and URL changes in address bar). In case you want to rewrite you should not add the domain again, in case you want to redirect add http:// as well. Assuming a rewrite is what you want use below rule:
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
</conditions>
</rule>

Globally modify left part URL in MVC 4 / 5

I am using IIS 8.5 with MVC4 and MVC5.
Let´s assume that I have two websites where the URL of the first (WS1) is:
http://myapp.example.com
and the second website (WS2) URL is:
http://myapp.example.com/sub
(and is located on another port to which I want to forward)
Now, my problem is that all URLs generated by MVC (links etc) will use the left part of the URL and remove /sub within that application. Let´s say for instance that in WS2 I want to redirect the user to http://myapp.example.com/sub/login but it will become http://myapp.example.com/login instead.
I have tried IIS URL rewrites but it will only work first-time request that actually contain the /sub part, the URLs generated by MVC of course remains erroneously without the /sub part.
<rewrite>
<rules>
<rule name="Rewrite">
<match url="^Sub/(.*)" />
<action type="Rewrite" url="http://myapp.example.com/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="Response Status Update" preCondition="ResponseStatus" stopProcessing="true">
<match serverVariable="RESPONSE_Location" pattern="^/(.*)" />
<action type="Rewrite" value="/Sub/{R:1}" />
</rule>
<rule name="RewriteRelativePaths" preCondition="IsHTML">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
<conditions>
<add input="{URL}" pattern="^/Sub/.*" />
</conditions>
<action type="Rewrite" value="/Sub/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="ResponseStatus">
<add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
My question is how I can accomplish this. Using another subdomain is not an option.

ASP.NET MVC 2: Make culture dependent redirect before routes kick in. For example: /Home => /en/us/Home

I want to make some redirects:
/Home => /en/us/Home
/ => en/us/
Where should I put these redirects and how should I redirect to avoid getting an infinite loop?
Instead en/us I want to extract the values that the users browser is submitting.
Thank you for your Help!
These things go into web.config. Look at this article. This is for IIS7, but I think II6 supports this as well.
In your case you would add something like this into web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="rule1" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^en/us/?" negate="true" />
</conditions>
<action type="Redirect" url="/en/us/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
This will catch all your non /en/us URLs and redirect them to the URL that starts with /en/us/ and what ever was in the original URL.
Note that this does not have the ability to test for actual location of the user. For that you'd probably have to use some logic.

Resources