IIS 7 URL Rewrite Module - asp.net

I can see the Outbound URL Rewrite working, because it replaces all the URL's on the page with the new URL. So wherever I put this:
/post.asp?topic=question&id=123
it rewrites it to...
/question/123
But, when I visit the rewritten URL /question/123 I get a 404 error. It looks like the rewriting part works, but how about loading the URL once you goto the rewritten URL?
I used the 'Create Friendly URL' wizard in IIS > URL Rewriting and checked the Outbound and Redirect rule as well., just like this example explains: http://learn.iis.net/page.aspx/497/user-friendly-url---rule-template/
Here is the Web.Config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^post\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^topic=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/post.asp?topic={R:1}&id={R:2}" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)/post\.asp\?topic=([^=&]+)&(?:amp;)?id=([^=&]+)&(?:amp;)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>

Have you modified the pattern(s)? I've created rules with same steps using wizard. However my configuration file is different than yours, it works smoothly. Compare the patterns, especially for OutboundRewriteUserFriendlyURL1.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^post\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^topic=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="post.asp?topic={R:1}&id={R:2}" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)post\.asp\?topic=([^=&]+)&(?:amp;)?id=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>

Related

URL Rewrite IIS and Wordpress

I have this following web.conf
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
<defaultDocument>
<files>
<clear />
<add value="index.php" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
<add value="default.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
The wordpress site works, however some images I get the following error if I view an image example http://domain/wp-content/uploads/2018/06/home-slide-5.jpg
HTTP Error 500.50 - URL Rewrite Module Error.
The page cannot be displayed because an internal server error has occurred.
I have added the following to the rules now it seems to work
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^index\.php$" ignoreCase="false"/>
<action type="None"/>
</rule>
<rule name="Redirect Image to HTTP" stopProcessing="true">
<match url=".*\.(gif|jpg|jpeg|png|css|js)$" ignoreCase="true"/>
<action type="Rewrite" url="{R:0}"/>
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="." ignoreCase="false"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true"/>
</conditions>
<action type="Rewrite" url="/index.php"/>
</rule>
CREDITS: Wordpress permalinks on IIS?

IIS web.config redirect rule for specific URL but not others

I want to add a redirect rule to the web.config file http://myurl.com/path/to/old
to http://myurl.com/path/to/new
but http://myurl.com/fr/path/to/old
http://myurl.com/cn/path/to/old
should stay the same. how can i achieve that with match/rule?
Add this to your web.config:
<rewrite>
<rules>
<rule name="Redirect old to new" stopProcessing="true">
<match url="^path/to/old" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^fr/.*" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^cn/.*" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/path/to/new" />
</rule>
</rules>
</rewrite>

Removing index.cfm from url with web config

quick question -
Currently my urls look like this: index.cfm/camp/another-test
I would like for them to look like this: camp/another-test
I'm able to do this fine on apache with my .htaccess but I need to be able to do it on iis7 with the web.config. Here's my rewrite so far:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
Thanks for the help!
I believe CFWheels requires that you route rewrite requests through rewrite.cfm not index.cfm.
See the comment by Chris Peters on this question
If you adjust:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
to:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" matchType="Pattern" ignoreCase="true" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
it should solve your problem, provided you have:
<cfset set(URLRewriting = "On")>
within /config/settings.cfm
Try adding this rewriting rule:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|newsletters|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>

Configure IIS URL Rewrite module for AngularJS HTML5Mode Still Not Working

I have searched at found some info on configuring IIS URL Rewrite module to work with AngularJS HTML5Mode and even tried the info I found on another question:
How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?
I've made the changes to my IIS as displayed in the question found in the link above and set the base URL and the site works when I visit the root site (i.e. http: //localhost/appname/). All of my views and the routing works fine.
However; when I try to visit a specific URL (i.e. http: //localhost/appname/about) in a new tab or browser window, I get a 404. Is there something else I'm missing???
I'm running Windows 7, IIS 7 with the URL Rewrite Module installed.
** I had to space the protocol out to allow the example URL to be accepted.
Install this extension: https://www.iis.net/downloads/microsoft/url-rewrite
And add this to web.config under system.webServer
<rewrite>
<rules>
<clear />
<rule name="Html5Mode" enabled="true" stopProcessing="true">
<match url="^(.+)$" negate="true" />
<conditions>
<add input="{REQUEST_URL}" pattern="^(.+)$" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" logRewrittenUrl="true"/>
</rule>
<rule name="AngularJS" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
Here is the full web.config including serving static files.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Html5Mode" enabled="true" stopProcessing="true">
<match url="^(.+)$" negate="true" />
<conditions>
<add input="{REQUEST_URL}" pattern="^(.+)$" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" logRewrittenUrl="true"/>
</rule>
<rule name="AngularJS" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
<handlers>
<clear />
<add
name="StaticFile"
path="*" verb="*"
modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"
resourceType="Either"
requireAccess="Read" />
</handlers>
<staticContent>
<mimeMap fileExtension=".*" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>

Redirection Rule Using web.config

I want to redirect
http://www.mydomain.com/rakhi-blog/index.php/2012/06/20/my-article/ to
http://www.mydomain.com/rakhi-blog/2012/06/20/my-article/
For this I use the following code:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<remove value="index.php" />
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
<rule name="Rewrite Index">
<match url="^index.php/*" />
<action type="Redirect" url="/rakhi-blog/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But This Code is not working. Can Any one Help Me Pls.
try:
<rewrite>
<rules>
<rule name="Rewrite Index">
<match url="^rakhi-blog/index.php/(.+)$" />
<action type="Redirect" url="/rakhi-blog/{R:1}" />
</rule>
</rules>
</rewrite>
In the past, I used the instructions in the article "Using Failed Request Tracing to Trace Rewrite Rules" to find and resolve such issues.
Basically, you turn on tracing and see what and why IIS is actually redirecting (or not redirecting). You then can see whether your expectations match the actual results.

Resources