IIS URL Rewrite multiple parameters - url

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>

Related

Issue with URL Rewriting - IIS & MVC

I trying to build a rewrite rule to "Permanent Redirect" of a PHP page to MVC site. here is the URL of PHP site
/Home/new-age-xxx.php & url of MVC site to which I want user to redirect "/home/new-age-xxx"
here is my URL rewriting rule
<rule name="Imported Rule 1-599" stopProcessing="true">
<match url="^home\new/-age/-xxx\.php$" ignoreCase="false" />
<action type="Redirect" url="/home/new-age-xxx" appendQueryString="false" redirectType="Permanent" />
</rule>
It is not redirecting to /home/new-age-xxx, even i tried to move it to other urls like /login/index
Any suggestion?
Thanks
You could use a bewlo url rewrite rule to redirect one site url to another site:
e.g:
Request URL: http://www.sample2.com/home/new-age-123.php
Redirect url:http://www.example.com/home/new-age-123
<rule name="reg redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="/home/new\-age\-([0-9]{1,3}).php$" />
</conditions>
<action type="Redirect" url="http://www.example.com/home/new-age-{C:1}" appendQueryString="false" />
</rule>
To learn more about url rewrite module you could refer the bewlo link:
Creating Rewrite Rules for the URL Rewrite Module
Regards,
Jalpa

Web requesting forwarding to another domain

I have a site "MySite" setup in my IIS with the below HTTP bindings,
www.example.com:80
www.example.net:80
www.example.org:80
I need to temporarily redirect all users coming to www.example.net to www.example.com/product.
Any ideas how to achieve this ?
Hi Siva Sankar Gurram,
You could use below url rewrite rule to redirect url www.example.net to www.example.com/product by using below url rewrite rule in iis:
<rule name=".net to .com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.net$" />
</conditions>
<action type="Redirect" url="http://www.example.com/product" redirectType="Temporary" />
</rule>
Note: If you did not installed Url rewrite module first install it.

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 !

IIS rewrite - redirect path to a different URL, including partial path (C# web config)

I have a multisite setup in Sitecore, where my main site is mysite.com and I have a secondary site of secondsite.org. These both work, but the user can also navigate to secondsite.org by going to the Sitecore path for the second site home node, i.e. mysite.com/second-site-home. I don't want this to happen, so I added a redirect in the webconfig so that mysite.com/second-site-home will redirect to secondsite.org:
<rule name="homepage path Redirect" stopProcessing="true">
<match url="^second-site-home/?(.*)$" />
<action type="Redirect" url="http://secondsite.org" redirectType="Found"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mysite.com$" />
</conditions>
</rule>
This works fine for the homepage, but I have links on mysite.com that go to pages on secondsite.org, and these links are rendering as mysite.com/second-site-home/about, and when I navigate to this link I get redirected to the secondsite homepage, rather than the actual page I want to go to.
Is is possible to modify the redirect so that in the redirect url, it includes the rest of the path after /second-site-home/ ?
You just need to add a back reference variable to your rule:
<rule name="homepage path Redirect" stopProcessing="true">
<match url="^second-site-home/?(.*)$" />
<action type="Redirect" url="http://secondsite.org/{R:1}" redirectType="Found"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mysite.com$" />
</conditions>
</rule>

IIS URL Rewrite not working with query string

I thought this was pretty straightforward, but it refuses to work. The old URL is
http://www.site.com/?q=node/17
It needs to redirect to http://www.site.com. I don't need to worry about wildcards, this is the only query string parameter I need to worry about. The rule I wrote looks like
<rule name="Node17" patternSyntax="ExactMatch" stopProcessing="true">
<match url="http://www.site.com/?q=node/17" />
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>
I can test the pattern inside of IIS and it matches, but when I hit the URL in a browser it doesn't redirect. Any thoughts?
As described in Microsoft's documentation:
It is important to understand how certain parts of the URL string can
be accessed from a rewrite rule.
For an HTTP URL in this form:
http(s)://{host}:{port}/{path}?{querystring}
The {path} is matched against the pattern of the rule. The
{querystring} is available in the server variable called QUERY_STRING
and can be accessed by using a condition within a rule.
Rule conditions allow defining additional logic for rule evaluation...
Rule conditions are evaluated after the rule pattern match is successful.
In the URL you wanted to rewrite as a redirect, your {host} = "www.site.com", {path} = "" and {querystring} = "q=node/17". So the {path} part in the URL you wanted to redirect is actually empty, and the rule you used in your question was matched against it and did not match.
Your solution is indeed valid, so I'll quote it here:
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.example.com" appendQueryString="False" />
</rule>
Of course I figured it out soon after I posted. This does it, not really sure why the exactmatch wasn't working though.
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

Resources