web.config: URL rewrite to remove directory - iis

So currently I've got the URL websitename.com/posts/post
and I would like to change it to websitename.com/post
I've been trying now for 3 days with no luck. Seen loads of stuff for .htaccess but not much for web.config
Apologies in advance as I'm sure I'm asking a really dumb question,
Thank you!

Maybe this is what you want. Put it in the root directory of your website. You could also change Rewrite to Redirect there.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="posts" patternSyntax="ExactMatch">
<match url="/posts/post" />
<action type="Rewrite" url="/post" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

web.config rewrite from one directory to another

I'm struggling to transform an Apache .htaccess rewrite to an IIS rewrite. I would like to rewrite from one directory to another.
Example: If someone accesses the URI: /booklets/MyPDF.pdf then the rewrite would access the file using /res/pdf/MyPDF.pdf
This is what I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite for booklets location" enabled="true">
<match url="^booklets/(.*).pdf" />
<action type="Rewrite" url="res/pdf/{R:1}.pdf" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Any help would be greatly appreciated.
In addition to your own answer i would suggest you to add stopProcessing attribute and backslash dot symbol before extension in regular expression:
<rule name="Rewrite for booklets location" stopProcessing="true">
<match url="^booklets/(.*)\.pdf$" ignoreCase="true"/>
<action type="Rewrite" url="res/pdf/{R:1}.pdf"/>
</rule>
After tinkering around with it, I just removed enabled="true" from the rule and added the ending regex $ to the url match, and now it works!
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite for booklets location">
<match url="^booklets/(.*).pdf$" />
<action type="Rewrite" url="res/pdf/{R:1}.pdf" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS7 rewrite url subfolder to parent folder (root)

I have a website mywebsite.com and installed a cms into a sub folder: mywebsite.com/subfolder
I need to now move the cms to parent folder root and I would like url's to be rewritten automatically so when browsing mywebsite.com/subfolder/page1.php you get redirected to mywebsite.com/page1.php
so far the only suggestions I found were redirecting any request to the root folder. In my case I need to rewrite.
Thanks!
I guess the reason why your question is so old and has not been answered is because it is not clear what you are asking for. Using the IIS7 rewrite module is easy, and I will provide a couple of different examples for what I think you are asking (either or).
So if you would like to "rewrite" urls that are in a subfolder but have them appear as if they were in the root, the following would work -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root_URL_Rewrite" stopProcessing="true">
<match url="^(.*)" />
<action type="Rewrite" url="/subfolder/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
If however you would like url's in an old folder (say url's that have been indexed by google) to be redirected (as a permanent 301 redirect) you could use the following example -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect_Root_URL" stopProcessing="true">
<match url="(.*)subfolder(.*)" ignoreCase="true" />
<action type="Redirect" url="{R:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Or, if like me you stumpled across this page and are wanting to do a rewrite to a subfolder, and then do a permanent 301 redirect from the old subfolder to the root, you might want something more similar to the following -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect_Root_URL" stopProcessing="true">
<match url="(.*)subfolder(.*)" ignoreCase="true" />
<action type="Redirect" url="{R:2}" redirectType="Permanent" />
</rule>
<rule name="Root_URL_Rewrite" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{URL}" pattern="^/subfolder/.*" negate="true" />
</conditions>
<action type="Rewrite" url="/subfolder/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

URL Rewrite IIS - Map From One Path To Another

I am playing with URL rewrite in IIS 7
The behaviour I want is when someone types in
[http://localhost/Sales]
they get redirected to [http://localhost/SalesDemo]
but they still see [http://localhost/Sales] in the browser URL
Is this possible?
The best way to achieve that would be to use Rewrite Maps in URL Rewrite Module.
Alternatively you could add rewrite section to your web.config file.
Web.config example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite rule">
<match url="^Sales$" />
<action type="Rewrite" url="SalesDemo" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Please note that the action type needs to be Rewrite (and not Redirect) if you still want to see /Sales in your browser.
I hope that will help.

IIS UrlRewrite - Only works one actual file exists

I have the following url rewrite code in my web.config
I want to forward anything .htm to the brand.aspx.
so if anyone type in
test1.htm
test2.htm
test3.htm
would go to the brand.aspx. However, the following url rewrite would work if there is an actual file call "test1.htm" in the server, then it will redirect to the brand.aspx. If there is no file exists, it will just return 404 instead of redirect to the brand.aspx. Does anybody know why? thanks a lot.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="false">
<match url="(.*)\.htm$" ignoreCase="true" />
<action type="Rewrite" url="brand.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
There is a similar thread
Using a custom URL rewriter, IIS6, and urls with .htm, .html, etc
I have experienced a similar issue before and i made a custom IHTTPModule
for which i used the guide at
http://msdn.microsoft.com/en-us/library/ms227673.aspx

IIS7 URL Rewriting to different domain, exact match

I basically want to match the exact address
http://www.example.com/mysite
and redirect it to
http://www.example2.com/something/something
If possible I want to be able to do it with IIS because I have coded an internal rewriting module for example.com that rewrites user friendly URLS to aspx pages, and I don't want any interference with the other site.
NINJA EDIT:
I want to keep the address as http://www.example.com/mysite so I need to rewrite it not redirect it.
This should do the job:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect mysite" stopProcessing="true">
<match url="^mysite$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example2.com/something/something" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Resources