IIS Rewrite Rule for adding versioning to the url if not found - iis

How can convert url www.mydomain.com/home to www.mydomain.com/api/v1/home
if the requested api link don't contain api/v1
I need someone help to write iis rewrite rule in web.config to fit this case
best regards

if the requested api link don't contain api/v1
You can use the parameter ^home$ to make the requested url match only home, this way api/v1 is not contained.
<rewrite>
<rules>
<rule name="Test" stopProcessing="true">
<match url="^home$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.domainname.com$" />
</conditions>
<action type="Redirect" url="http://www.domainname.com/api/v1/home" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Body cannot contain mydomain, so i use domainname instead of it.

Related

How to add rules in Url redirect in iis 10

I want to write redirect rules in IIS 10. I googled it and spent half of my day figuring out but no luck so I am posting it to get some solution.
https://testing.app.com/apptest should redirect to https://testing.app.com/apptest/account/login
https://testing.app.com/apptest/ should redirect to https://testing.app.com/apptest/account/login
https://test-apptest.testing.app.com/ should redirect to https://test-apptest.testing.app.com/account/login
https://test-apptest.testing.app.com should redirect to https://test-apptest.testing.app.com/account/login
But when user types url https://testing.app.com/apptest/account/login or https://test-apptest.testing.app.com/account/login then it should not redirect anywhere and it should stay as it is.
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="account/login" />
<action type="None" />
</rule>
<rule name="Test2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest" />
</conditions>
<action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
We just add an anchor point to the regular expression so that precisely matches the segment ‘/apptest’.
Updated
<system.webServer>
<rewrite>
<rules>
<rule name="MyRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
</conditions>
<action type="Redirect" url="https://{http_host}{C:1}/account/login" />
</rule>
</rules>
</rewrite>
</system.webServer>
Explanation
Since the hostname changed and will subsequently be appended in the redirection URL, I replace it with {http_host} server variable to follow it. Besides, {Request_URI} will return the URL path and {C:1} will return either "/apptest" or "". therefore I append it into the redirection URL.
The meaning of every server variable.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
Finally, please don’t forget to install the URL Rewrite extension before applying the rules.
https://www.iis.net/downloads/microsoft/url-rewrite
Here is a quick reference of the regular expression from Microsoft documentation.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Feel free to let me know if the problem persists.

URL Rewrite rule for a specific domain

URL Rewrite rule for a specific domain
if url is https and have this domain only
https://myServer/SomeApplication/
Redirect it to
https://myServer.mycompany.com/SomeApplication/
Added below didn't work in iis 10, windows server 2019
<rule name="httpsRedirect2" enabled="true" stopProcessing="true">
<match url="^myServer/(.*)" ignoreCase="true"/>
<action type="Redirect" url="https://myServer.mycompany.com/SomeApplication/" appendQueryString="false" />
</rule>
Can someone explain what I've done wrong?
The Rule pattern only can get the URL string as an input, does not include the query string, thus it will not work properly.
Here is an official explanation of how certain parts of the URL string can be accessed from a rewrite rule.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
We can match the hostname in the Rule Condition section then apply the Rule Action.
<rule name="Myrule2" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" />
</conditions>
<action type="Redirect" url="https://vabqia969vm/" />
</rule>
</rules>
</rewrite>
Feel free to let me know if the problem still exists.

IIS rewrite rule not triggered when file-name plus extension is in the url

I am struggling getting an IIS rewrite rule working.
The old urls look like:
https://wwww.testserver.com/package.aspx?TrackingNumber=number
The new web app is expecting the urls in this format:
https://wwww.testserver.com/package/number
Currently I have the following rule setup, which is not working.
<rule name="Rewrite to new Package site" stopProcessing="true">
<match url="^package\.aspx\?TrackingNumber=([_0-9a-z-]+)" />
<action type="Redirect" url="https://www.testserver.com/package/{R:1}" logRewrittenUrl="true" />
</rule>
The interesting part is, if I remove package.aspx\? from the rule, urls like these https://wwww.testserver.com/oldwebsite/TrackingNumber=number are getting matched.
In my test calls I replaced package.aspx with package.html and these urls are not getting matched as well. It looks like IIS ignores urls with filenames in the url.
You could use below url rewrite rule to redirect https://wwww.testserver.com/package.aspx?TrackingNumber=number to https://wwww.testserver.com/package/number:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="package(.aspx)" />
<add input="{QUERY_STRING}" pattern="TrackingNumber=(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:2568/package/{C:1}" appendQueryString="false" />
</rule>
Note: You could modify hostname as per your requirement.
Regards,
Jalpa

IIS URL Rewrite rule using request header

I have been trying to creat an IIS Rewrite rule that looks at the incoming Header for an API and if it contains a certain string re-direct them to a certain page and not the API (Noobs - Using documentation API Keys)
Been at it 2 hours and just can't work it out. Could anyone help?
Thanks
Finally Got it! Here is teh rule below for anyone else that is interested.
<rewrite>
<rules>
<rule name="Rewrite Noob Documentation API Key" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_Authorization}" pattern="^ukvd-ipwhitelist ABCD1234-1b3d-4d63-aa75-ABCDEF123456$" />
</conditions>
<action type="Redirect" url="https://xxx.co.uk/dockey.html" appendQueryString="false" />
</rule>
</rules>
</rewrite>

URL Alias in IIS 8.5 Using URL Rewrite Module

I need to create easy to remember URL's that redirect to long and hard to remember paths for a large number of applications.
i.e.
subdomain.domain.edu/shortname
redirects to
https://www.subdomain.domain.edu/mainApplication/subfolder/page
I'm using the URL Rewrite module in IIS 8.5, but I keep getting a 404 when I browse to the short alias. I know the rewrite module is working as I use it to handle rewriting HTTP to HTTPS and to add WWW to a URL.
My rewrite rule looks like:
<rewrite>
<rules>
<rule name="Easy to remember shortcut" stopProcessing="true">
<match url=".*subdomain.domain.edu/shortname" />
<conditions>
<add input="{URL}" pattern=".*/shortname" />
</conditions>
<action type="Redirect" url="https://www.subdomain.domain.edu/mainApplication/subfolder/page.aspx" />
</rule>
</rules>
</rewrite>
Of course this returns a 404. Any ideas?
Forgive me if there is already an answer to this in another post, however, I've read through and tried over 30 posts on the URL rewrite module and have not yet found the solution for actually creating an alias.
Url rewrite match condition won't be able to see your domain i.e. if the URL is https://www.subdomain.domain.edu/shortname the match part can only see shortname (anything after domainname/).
To validate the host we need to add in the conditions clause. So your rule will be something like below
<rule name="shortnameURL" enabled="true" stopProcessing="true">
<match url="shortname" />
<action type="Redirect" url="mainApplication/subfolder/page" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*subdomain.domain.edu" />
</conditions>
</rule>
Also it should be ok if you add the entire URL here
<action type="Redirect" url="mainApplication/subfolder/page" /> as below
<action type="Redirect" url="https://www.subdomain.domain.edu/mainApplication/subfolder/page" />

Resources