IIS Rewrite: Match only if first thing after domain - iis

I have an IIS rewrite rule written like so:
<rule name="Rewrite" stopProcessing="true">
<match url="foo/(.*)" />
<action type="Redirect" url="bar/{R:1}" />
</rule>
However, I would like to modify this so it onyl matches if 'foo' is the first thing after the domain.
So for example
www.example.com/foo/a/b/c
should redirect to
www.example.com/bar/a/b/c
But
www.example.com/bar/a/b/foo/c/d
Should NOT get redirected. Currently my rule changes the above to
www.example.com/bar/c/d

In this case I just needed to add a ^ so the rule becomes:
<rule name="Rewrite" stopProcessing="true">
<match url="^foo/(.*)" />
<action type="Redirect" url="bar/{R:1}" />
</rule>

Related

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 url rewrite with or without query

I have iis rewrite rule as follows.
<rule name="Redirect url1" stopProcessing="true">
<match url="^nsg/([!-~]+)" />
<action type="Redirect" url="https://test.com/gsl/{R:1}"
appendQueryString="true" redirectType="Permanent" />
</rule>
Which is successful when I type in https://test.com/nsg/tasksearch.aspx, it will redirect to https://test.com/gsl/tasksearch.aspx. However, I also want it
when I just type in https://test.com/nsg, it can redirect to https://test.com/gsl. Can I do that?
Just a simple change from your regular expression. Please try the below rules.
<rewrite>
<rules>
<rule name="MyRule" stopProcessing="true">
<match url="^nsg([!-~]*)" />
<action type="Redirect" url="https://test.com/gsl{R:1}" />
</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 Rewrite - Remove real folder from the URL

Please, I have this folder:
root/teste_seo/script.asp
Instead to use the url:
www.mysite.com/teste_seo/script.asp
I need this url using IIS rewrite:
www.mysite.com/script.asp
I try to use the rule from this link but didn't work:
IIS 7.5 URL Rewrite - Rewrite a Folder from an URL
Please, how can I write this rule?
PS.: For any *.asp file inside teste_seo folder.
The following rule should do the trick:
<rule name="teste_seo" stopProcessing="true">
<match url="^script.asp" />
<action type="Rewrite" url="/teste_seo/script.asp" />
</rule>
Then just link to the www.mysite.com/script.asp
Edited after comment from OP:
If so you can use the following rule as long as you have a copy of the script files in the root of your document:
<rule name="teste_seo" stopProcessing="true">
<match url="^teste_seo/([^\.]+\.asp)" />
<action type="Redirect" url="/{R:1}" />
</rule>
And if you need all script files to point to the same one you can use:
<rule name="teste_seo" stopProcessing="true">
<match url="^teste_seo/([^\.]+\.asp)" />
<action type="Redirect" url="/script.asp" />
</rule>
Edited again after clarification:
These 2 rules should then be the fix you need:
<rule name="teste_seo_redirect" stopProcessing="true">
<match url="^teste_seo/([^\.]+\.asp)" />
<action type="Redirect" url="/{R:1}" />
</rule>
<rule name="teste_seo_rewrite" stopProcessing="true">
<match url="^([^\.]+\.asp)" />
<action type="Rewrite" url="/teste_seo/{R:1}" />
</rule>

Remove a part of url with rewrite

I'm not familiarized with this feature and I've found some examples how to do something but I don't know how to set this case:
I want this url http://www.domain.co.uk/anything/en to become http://www.domain.co.uk/anything/
I want to remove the final 'en'
I've tried with:
^/([_0-9a-z-]+)/([_0-9a-z-]+)/en
/{R:1}/{R:2}
or
^http://www.domain.co.uk/([_0-9a-z-]+)/(en)
http://www.domain.co.uk/{R:1}/
But it doesn't work.
You can use the following rule:
<rule name="Remove en" stopProcessing="true">
<match url="^(.*)/en$" />
<action type="Rewrite" url="{R:1}" />
</rule>
It will match any url ending with /en and remove this part.
From your example, http://www.domain.co.uk/anything/en will be rewritten as http://www.domain.co.uk/anything
If you want the user to be redirected, then use the following rule:
<rule name="Remove en" stopProcessing="true">
<match url="^(.*)/en$" />
<action type="Redirect" url="{R:1}" />
</rule>
The type="Redirect" with no option triggers a permanent(301) redirect.

Resources