URL Redirection using IIS 7 - iis

I wish to redirect ALL pages from a specific folder and its subfolders to a page on root folder. I have written the following rule in IIS 7.5 but it is not working. Please advise.
<rule name="someName" stopProcessing="true">
<match url="/MoveFromThisFolder/.*" />
<action type="Rewrite" url="/ToThisFile.html" appendQueryString="false" />
</rule>

I modified the rule as follows and it worked for me!
<rule name="SomeRule" stopProcessing="true">
<match url="^FromThisFolder/(.*)" />
<action type="Redirect" url="ToThisFile.html" appendQueryString="false" />
</rule>

Try getting rid of the leading "/"
<rule name="someName" stopProcessing="true">
<match url="^MoveFromThisFolder/($|[\?/].*)" />
<action type="Rewrite" url="ToThisFile.html" appendQueryString="false" />
</rule>

Related

IIS URL Rewrite: Match URL with and without Slash

I want to redirect specific URLs permanently to other domains.
<rule name="URL Test" enabled="true" stopProcessing="true">
<match url="^en/career/jobs$" />
<conditions logicalGrouping="MatchAny">
</conditions>
<action type="Redirect" url="https://www.example.com/newjobs" appendQueryString="false" redirectType="Permanent" />
</rule>
It works when I use https://source.com/en/career/jobs
but when I add a slash in the end (https://source.com/en/career/jobs/), it doesnt work.
How can I accept both variants in the match url ?
You just need to change your parameters to match both en/career/jobs/ and en/career/jobs. You can use the following rules as a reference:
<rule name="URL Test" enabled="true" stopProcessing="true">
<match url="^en/career/jobs(.*)$" />
<action type="Redirect" url="https://www.example.com/newjobs" appendQueryString="false" redirectType="Permanent" />
</rule>

How to write rules under URL redirect in IIS 10

I want to write redirect rules in IIS 10. I googled it but could not found proper solution.
I have added some more scenarios.
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
Appreciated if someone can please help me with this.
What I tried so far is:
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="apptest/login" />
<action type="None" />
</rule>
<rule name="login">
<match url="^apptest" />
<action type="Redirect" url="testing.app.com/apptest/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
You can try the rule below:
<rule name="test5" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest$" />
</conditions>
<action type="Redirect" url="https://www.google.com/" appendQueryString="false" />
</rule>

IIS URL Rewrite remove part of path

After researching url rewrite and reviewing many posts, I'm still stumped as to why my url rewrite is not working. I am attempting to remove /carrot/ from the path.
Ex: https://my.server.com/carrot/mobile/path
should become: https://my.server.com/mobile/path
My URL rewrite rule is pretty simple and looks as follows:
<rule name="RemoveCarrotFromPath">
<match url=".*carrot(.*)" />
<action type="Rewrite" url="{R:1}" />
</rule>
All help is appreciated.
Edit Below you can find all rules in use in case this is an issue where various rules are clashing:
<rewrite>
<rules>
<rule name="redirectPayment" stopProcessing="true">
<match url="/payment" />
<action type="Redirect" url="https://my.app.com/carrot/Payment" />
</rule>
<rule name="redirectMembership" stopProcessing="true">
<match url="/membership" />
<action type="Redirect" url="https://my.app.com/carrot/Membership" />
</rule>
<rule name="RemoveCarrotFromPath">
<match url=".*carrot(.*)" />
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
The solution to my issue was to use Redirect instead of Rewrite as follows:
<rule name="RemoveCarrotFromPath">
<match url=".*carrot(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>

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>

URL rewrite and redirect between hosts

I have the following rewrite rule to redirect any conetnt from a host, hostA.com to the home page of a new host, hostB.com. I also want to browser url to change to www.HostB.com
<rewrite>
<rules>
<rule name="301 redirect entire site" stopProcessing="true">
<match url="^hostA(.*)$" />
<action type="Redirect" redirectType="Permanent" url="http://www.hostB.com" appendQueryString="true" />
</rule>
</rules>
</rewrite>
But that does not work. How can I fix this please.
you can doit like this
<rule name="domain redirect" enabled="true" stopProcessing="true">
<match url="^hostA(.*)$" />
<action type="Redirect" url="http://hostb.example.com/{R:0}" appendQueryString="true" />
</rule>

Resources