Remove a part of url with rewrite - iis

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.

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.

How to REWRITE a folder name in web.config with a different name?

I know my way around .htaccess files a bit but not really web.config files. There are many questions about this on here and I hate to ask a duplicate but I haven't been able to get it to work.
Here's what I am trying to do.
I need to convert this URL:
https://www.website.com/some-stuff-here/replacethis/some-more-stuff-here
To this URL:
https://www.website.com/some-stuff-here/foobar/some-more-stuff-here
I need to replace replacethis with foobar
Here's what I've tried:
<rule name="Rewrite for Foobar">
<match url="^replacethis/(.*)" />
<action type="Rewrite" url="^foobar/{R:1}" />
</rule>
<rule name="Rewrite for Foobar">
<match url="^/(.*)/replacethis/(.*)" />
<action type="Rewrite" url="^/{R:1}/foobar/{R:2}" />
</rule>
I don't know what I'm doing help me. :(
According to your description, I suggest you could try to use below url rewrite rule to achieve your requirement.
Notice: you shoud replace the localhost url to your example url.
<rule name="Rewrite for Foobar1">
<match url="^replacethis/(.*)" />
<action type="Rewrite" url="http://localhost:8082/foobar/{R:1}" />
</rule>
<rule name="foldername">
<match url="(.*)/replacethis/(.*)" />
<action type="Rewrite" url="http://localhost:8082/{R:1}/foobar/{R:2}" />
</rule>
Result:

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: Match only if first thing after domain

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>

Resources