IIS URL Rewriting - Rule captures despite file existing - iis

I've got a rule that rewrites URLs unless a physical file exists (so static files can be returned) - however for some reason the rules get rewritten anyway, to much frustration.
Here's my Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Front Page">
<match url="/?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="/Home/FrontPage" />
</rule>
<rule name="Map Everything" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="EntryPoint.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>

Ah, it turns out the "Front Page" rule was the problem, and the rule "/?" was matching everything when I should have used "^$" instead.
After fixing that, the IsFile/IsDirectory rules were being respected.

Related

IIS Rewriting: Why it not returning fresh data

I am using the following rewriting rule to get the data from the other server, and it is returning the result for the first time. But when I am updating/adding the data, my API is still returning the previous data(no updated data). But after approx 30 seconds when I again hit the API, It returns updated data.
It's working fine when I am using a direct URL(http://172.20.20.23:3000/).
I don't know what am I missing. Please help me to identify the issue.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Frontend Router" 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="^/(qosapi)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="^qosapi/(.*)" />
<action type="Rewrite" url="http://172.20.20.23:3000/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
It was a reverse proxy caching issue. I just disabled the caching and it's working perfectly fine now.
See the below image:

URL friendly ASP Classic and Isapi Rewrite

The point is, I can access an address dominio.com/modulo/id/titulo and it rewrites to dominio.com/default.asp?link=artigo&id=123&titulo=teste, but my question is whether I can do the reverse process, i.e. go to dominio.com/default.asp?link=artigo&id=123&titulo=teste and it changes to dominio.com/modulo/id/titulo.
Codes:
ASP
<!DOCTYPE html><html lang="pt-br"><head><meta charset="utf-8"/><title>Teste Isapi Rewrite</title></head><body><p>Teste!<br>link: <%=request("link")%><br>id: <%=request("id")%><br>teste: <%=request("teste")%><br></p></body></html>
WEB.CONFIG
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="artigo" stopProcessing="true">
<match url="^artigo/?([a-zA-Z0-9_-]+)?/?([a-zA-Z0-9_-]+)?/?([a-zA-Z0-9_-]+)?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.asp?link={R:0}&id={R:1}&teste={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You could use below url rewrite rule:
<rule name="reverse" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="default.asp" />
<add input="{QUERY_STRING}" pattern="link=(.*)\&id=(.*)\&titulo=(.*)" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" enabled="true" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.asp?link={R:1}&id={R:2}&titulo={R:3}" appendQueryString="false" />
</rule>
default page code:
<!DOCTYPE html><html lang="pt-br">
<head>
<meta charset="utf-8"/>
<title>Teste Isapi Rewrite</title>
</head>
<body>
<p>Teste!<br>link: <%=request("link")%><br>id: <%=request("id")%><br>titulo: <%=request("titulo")%><br></p>
</body>
</html>
What I think you're after is a rule which redirects the "unfriendly" URL to the "friendly" one - ie if you paste the unfriendly one into your browser address bar then it changes to the friendly one when your page appears in the screen. Janvi Panchal has the right idea above. What you do is have a redirect rule and a rewrite rule as follows.
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^dominio\.com/default\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^link=([^=&]+)&id=([^=&]+)&titulo=([^=&]+)$" />
</conditions>
<action type="Redirect" url="dominio.com/default/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^dominio\.com/default/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="dominio.com/default.asp?link={R:1}&id={R:2}&titulo={R:3}" />
</rule>
Note that the redirect rule comes first, so what's happening is that the location is redirected to the friendly URL which is then rewritten as the unfriendly one.
Also note that I didn't hand code this - I generated it with the URL rewrite wizard in IIS Manager. Click on the URL Rewrite icon, then Add Rule then User Friendly URL. Check the "Create corresponding redirect rule" before you click the OK button and this code will be generated and appended to your web.config file in the appropriate location

Redirect with bypassing having period (dot) and slash in URL

I have one url which having ./ [period & slash] at the end of parameter. I want to redirect that url with different location but its not even detecting in rules. I am using IIS. I want to configure this on web.config
http://somesitename.com/mypage/teachers-manual./sku/8772
needs to redirect on
http://somesitename.com/mypage/teachers-manual/sku/8772
Though I have tried solution given on Here but its not even working. But if I use same thing instead of Redirect with Rewrite then Rule start working. Not sure why its not working for "Redirect".
<rule name="Trailing Dots and spaces" stopProcessing="true">
<match url="^mypage\/(.*)([\.\s]+)\/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}/{R:2}/{R:4}" appendQueryString="true" />
</rule>
Actually when I tried to write rule then url which having ./ is also not working.[ http://somesitename.com/mypage/teachers-manual./sku/8772 ]
<rule name="Trailing Dots and spaces1.1" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://somesitename.com/newpage.html" />
</rule>
Not sure where its wrong.
Just got more information on Post & Haacked Said for it.
so I have modified file as follows and now its perfectly working for me.
<configuration>
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Trailing Dots and spaces1.1" stopProcessing="true">
<match url="^(.*)/(.*)\.\/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="/{R:1}/{R:2}/{R:3}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
.... etc

web.config URL rewrite not rewriting

I've been having a problem recently with one of my rewrite rules in web.config. It's giving me a 500 error, yet it used to work in the past. I have, however, recently had to change it over from .htaccess to web.config, so I feel that may be part of the problem, but it used to work even after changing it over so I don't know what the problem is?
The web.config is currently sat as just imported rules now (default after importing now) and the content is the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" enabled="false" stopProcessing="true">
<match url="^images/([a-zA-Z0-9_-]+\.)(jpg|png|gif)$" ignoreCase="false" />
<action type="Rewrite" url="images/old/{R:1}{R:2}" />
</rule>
<rule name="Imported Rule 2" enabled="false" stopProcessing="true">
<match url="^images/thumbs/([a-zA-Z0-9_-]+\.)(jpg|png|gif)$" ignoreCase="false" />
<action type="Rewrite" url="images/old/{R:1}th.{R:2}" />
</rule>
<rule name="Imported Rule 3" enabled="false" stopProcessing="true">
<match url="^api$" ignoreCase="false" />
<action type="Rewrite" url="api.php" />
</rule>
<rule name="Imported Rule 4" enabled="false" stopProcessing="true">
<match url="^images/(\w*\.)(jpg|png|gif)$" ignoreCase="false" />
<action type="Rewrite" url="images/old/{R:1}{R:2}" />
</rule>
<rule name="Imported Rule 5" enabled="false" stopProcessing="true">
<match url="^images/thumbs/(\w*\.)(jpg|png|gif)$" ignoreCase="false" />
<action type="Rewrite" url="images/old/{R:1}th.{R:2}" />
</rule>
<rule name="Imported Rule 6" enabled="false" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="\.([a-z]{1,4})$" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm not the best at web.config, so if you know any reason as to why it may not be working after importing, can you help out? Thanks very much!

IIS Rewrite rule with QueryString in Rewrite

I'm having a problem with an IIS rewrite rule. We have many set up and working in our web.config file. I am trying to modify one of them. Apologies for the level of this question. I'm not experienced in this topic.
The current rule is:
<rule name="my-rule-test" stopProcessing="true">
<match url="^my-page.asp" />
<action type="Rewrite" url="/my-domain/my-actual-page.asp" />
</rule>
This rule works fine. I'm now trying to simply add a QueryString to the Rewrite page as follows:
<rule name="my-rule-test" stopProcessing="true">
<match url="^my-page.asp" />
<action type="Rewrite" url="/my-domain/my-actual-page.asp?aS=h" />
</rule>
This rule fails and trips the server over to the page we have set up in web.config here:
<system.web>
<customErrors defaultRedirect="c:\test.html" mode="On">
</customErrors>
</system.web>
Can somebody please let me know what I'm doing wrong?
Thanks,
J
Your formatting is not correct:Please refer to this.
<rules>
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

Resources