Url rewrite to lower case not working - asp.net

I have the following rewrite rules in my web.config. The canonical rule works, however the lower case rule doesn't.
I'm trying to test it like this: www.mysite.com/UPPERCASE. I would have expected the url to be transformed to www.mysite.com/uppercase, but it stays in uppercase. What am I doing wrong?
<rewrite xdt:Transform="Insert">
<rules>
<rule name="LowerCaseRule" patternSyntax="ExactMatch">
<match url="[A-Z]" ignoreCase="false"/>
<action type="Redirect" url="{ToLower:{URL}}"/>
</rule>
<rule name="CanonicalHostName">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www.mysite.com$" negate="true" />
</conditions>
<action type="Redirect" url="{MapSSL:{HTTPS}}www.mysite.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapSSL" defaultValue="OFF">
<add key="ON" value="https://" />
<add key="OFF" value="http://" />
</rewriteMap>
</rewriteMaps>
</rewrite>

You should remove patternSyntax="ExactMatch" from the rule LowerCaseRule because in your case, you want to use the regular expression system (which comes by default or by setting patternSyntax="ECMAScript").
So your rule should be:
<rule name="LowerCaseRule">
<match url="[A-Z]" ignoreCase="false"/>
<action type="Redirect" url="{ToLower:{URL}}"/>
</rule>

Try this
<rule name="LowerCaseRule" stopProcessing="true">
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern=".*[A-Z].*" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>

This was better for me. I noticed {URL} does not properly resolve when you have a path like cassete.axd/scripts/myscript.js?xxx, it will redirect to cassette.axd?xxx instead.
<rule name="LowerCaseRule - HTTPS">
<match url="[A-Z]" ignoreCase="false"/>
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{ToLower:{HTTP_HOST}}{ToLower:{PATH_INFO}}" appendQueryString="true"/>
</rule>
<rule name="LowerCaseRule - HTTP">
<match url="[A-Z]" ignoreCase="false"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="http://{ToLower:{HTTP_HOST}}{ToLower:{PATH_INFO}}" appendQueryString="true"/>
</rule>
Hope this helps someone.

Related

Possible to exclude subfolder from https rewrite?

I have a webiste that runs https. Now some programmers need a subfoder that has no https.
This is what i have in my web.config right now:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
We need the folder "wp-content\rest" to be excluded from this https redirect.
In hope someone can help me with this one.
You need to add an additional condition into your rule:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/wp-content/rest" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Then this rule will disable redirects to all urls like that http://www.example.com/wp-content/rest*

Web.config URL rewrite - force www prefix and https

I'm trying to enforce https and a www prefix. However my rule doesn't fully work. Here is my rule:
<rewrite>
<rules>
<clear />
<rule name="Force https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
<add input="{HTTP_HOST}" pattern="www.mydomain.co.uk" negate="true" />
</conditions>
<action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It works for redirecting http to https.
it works if I go to https://mydomain.co.uk (redirects to https://www.mydomain.co.uk)
however it DOES NOT work if I go to https://mydomain.co.uk/blah/whatever
Please can somebody advise? Thanks.
These are the rewrite rules that I use for that exact purpose. I've also added a rule to make the URL all lowercase and a rule to remove the trailing slash should one be present. This makes working with Analytics easier since it treats page.aspx and page.aspx/ as different url's. That is why I use ignoreCase=true because then it does not matter if someone uses upper case somewhere since it will be handled later on by the ToLowerCase rule
<rule name="ForceWWW" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com" />
</conditions>
<action type="Redirect" url="https://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="HTTPtoHTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="RemoveTrailingSlash">
<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>
<rule name="ToLowerCase">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
<conditions>
<add input="{URL}" pattern="WebResource.axd" negate="true" />
<add input="{URL}" pattern="ScriptResource.axd" negate="true" />
</conditions>
</rule>
Here is an example of such web.config -- it will force HTTPS for ALL resources (using 301 Permanent Redirect):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<rule name="Redirects to www.example.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Source: https://stackoverflow.com/a/9823208/5740382
For more Details: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

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>

url rewrite - error This webpage has a redirect loop

I would like to redirect all traffice coming from http://www.example.com to http://www.mysite.com/badreferer.aspx?bad=true
for this i tried to create a rule in web.config file of http://www.mysite.com in IIS7.
<rule name="bad referer" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="(.*)example(.*)" />
</conditions>
<action type="Redirect" url="/badreferer.aspx?bad=true" appendQueryString="false" />
</rule>
But having issue with redirect loop.
Please help.
Thanks.
Try this:
<rules>
<rule name="bad referer" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="http://www.example.com(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/badreferer.aspx?bad=true" />
</rule>
</rules>
update:
<rule name="bad referer" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/badreferer\.aspx?bad=true" negate="true" />
<add input="{HTTP_REFERER}" pattern="^www.\example\.com.*" />
</conditions>
<action type="Redirect" url="/badreferer.aspx?bad=true" appendQueryString="false" />
</rule>

Web.Release.Config Rewrite URL Exclusion

I have found the following code which will redirect all my traffic to https:
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Is it possible to add an "exclusion" to this rule somehow?
I would like a particular path not to be redirected if possible
For example I would like everything below http://www.example.com/desktopmodules/ not to redirect.
Can it be done please?
You can add a rule above it which matches your exclusion pattern, ensuring that the stopProcessing="true" is set on the rule, but with no action:
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<!-- Prevent redirection of URLs matching this pattern -->
<rule name="ExcludeRedirectToHTTPS" stopProcessing="true">
<match url="^desktopmodules.*" />
<action type="None" />
</rule>
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

Resources