IIS Rewrite - Remove real folder from the URL - iis

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>

Related

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 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>

URL Redirection using IIS 7

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>

How to append query string & value via IIS rewrite rule?

I'm running IIS 7 with the offical rewrite rule module installed. I'd like to create a rewrite rule to match this URL:
http://www.sample.com/en-us/test.aspx?q=keyword
After rewriting the expected result would be:
http://www.sample.com/en-us/test.aspx?q=keyword&flag=value
How can I create a rule to implement this?
I've tested the following rule, but no luck, it always got redirect loop error:
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)/test\.aspx(.(?!flag=value))*$" />
<action type="Redirect" url="{R:0}&flag=value" appendQueryString="false" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
Found the solution by myself, just share it.
<rewrite>
<rules>
<rule name="Redirect for download result page" stopProcessing="true">
<match url="(.*)/test.aspx(.*)" />
<action type="Redirect" url="{R:1}/test.aspx?rf=sp" appendQueryString="true" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="flag=value" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>

Resources