Exclude path in IIS rewrite rule? - regex

I have a rewrite rule that converts a URL to lowercase. I would like to exclude a folder but don't know RegEx. How do I exclude "~/myfolder" from the rule below?
<rewrite>
<rules>
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
</rules>
</rewrite>

You could do something such as:
<rules>
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<conditions>
<add input="{URL}" negate="true" pattern="^~/myfolder$" />
</conditions>
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
</rules>
or... you could create another rule that does essentially the opposite for the specific match:
<rules>
<rule name="LowerCaseRule2" stopProcessing="false">
<match url="^~/myfolder$" ignoreCase="true" />
<action type="None" />
</rule>
</rules>

Related

IIS URL Rewrite Module - Regular Expression for URL

I would like to redirect the below old URL:
https://domain1.xyz.com/content/images/1ca9551bc00357f63c608b5d90ca7c4652187487.png
to the new URL:
https://domain2.xyz.com/originalfiles/1ca9551bc00357f63c608b5d90ca7c4652187487.png
My web.config file has below configuration but I cannot get it to work:
<rewrite>
<rules>
<rule name="Rewrite Images to CDN" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="https://domain2.xyz.com/originalfiles/{C:1}" appendQueryString="true" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain1.xyz.com" />
<add input="{URL}" pattern="content/images/(/*.*)" />
</conditions>
</rule>
</rules>
</rewrite>
Above configuration redirects to this URL which is incorrect:
https://domain2.xyz.com/originalfiles/content/images/1ca9551bc00357f63c608b5d90ca7c4652187487.png
Please try the following rule:
<rewrite>
<rules>
<rule name="Rewrite Images to CDN" enabled="true" stopProcessing="true">
<match url="content/images(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain1.xyz.com" />
</conditions>
<action type="Redirect" url="https://domain2.xyz.com/originalfiles{R:1}" />
</rule>
</rules>
</rewrite>

Web.config Rewrite rule in some cases, otherwise leave original

Can somebody help me write Rewrite rule that would basically say this:
If URL has "siteA" in it, but only if continues with "wp-admin, wp-content or wp-includes" then remove "siteA". In all other cases, leave "siteA" in URL.
e.g
domain.com/siteA/hello -> remains domain.com/siteA/hello
domain.com/siteA/wp-admin/some.css -> rewrites to domain.com/wp-admin/some.css
domain.com/siteA/wp-content/some.css -> rewrites to domain.com/wp-content/some.css
domain.com/siteA/wp-includes/some.css -> rewrites to domain.com/wp-includes/some.css
I tried several options, but I obviously suck at regex and url-rewriting; one of those tries:
<rule name="Custom: remove subsite from resources paths">
<match url="^siteA/(wp-(content|admin|includes).*)"/>
<action type="Rewrite" url="{R:1}"/>
</rule>
Tried also
<rule name="Custom: remove subsite from resources paths">
<match url="^siteA/(wp-(content|admin|includes))/(*)"/>
<action type="Rewrite" url="{R:2}{R:3}"/>
</rule>
and also
<rule name="Custom: remove subsite from resources paths">
<match url="^siteA/(wp-(content|admin|includes).*)"/>
<action type="Rewrite" url="{R:2}{R:3}"/>
</rule>
This is my complete rules node (generated by Wordpress due to the multisite).
So the thing is, it generates
domain.com/siteA/wp-includes/some.css
instead of
domain.com/wp-includes/some.css
So I need to lose the "siteA" when it comes to wp-includes etc. In other cases (regular post domain.com/siteA/regularPost ) should remain as is.
<rewrite>
<rules>
<rule name="WordPress Rule 1" stopProcessing="true">
<match url="^index\.php$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="WordPress Rule 2" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
<action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
</rule>
<rule name="WordPress Rule 3" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="WordPress Rule 4" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
<action type="Rewrite" url="{R:1}" />
</rule>
<rule name="WordPress Rule 5" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
<action type="Rewrite" url="{R:2}" />
</rule>
<rule name="WordPress Rule 6" stopProcessing="true">
<match url="." ignoreCase="false" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
I'm not sure what you want. According to your problem description, the several options you have tried almost achieve your goal. I tested it and it had some problems. So I improve it to rewrite siteA.
<rule name="remove siteA">
<match url="siteA/(wp-(content|admin|includes))/(.*)" />
<action type="Rewrite" url="{R:1}/{R:3}" />
</rule>
The process of rewrite can check in failed request tracing.

URL Rewrite iis server

I want to rewrite a url to index.html. the web.config below does the job until first path.
eg:- test.com/test1 to test.com/index.html but it doesnt work with multiple paths. eg:- test.com/test1/test2
my current web.config
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm not sure what I'm missing here. Please if any of you know, write the correct web.config.
Thank you.
You can try this rule:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/([^/]+)" />
</conditions>
<action type="Redirect" url="http://test.com/{C:1}/index.com" redirectType="Found" />
</rule>

IIS rewrite rule one query parameter with multiple values

IIS web.config:
I am trying to redirect a path like
mysite.com/?myparam=123,321,112
to
mysite.com/mypage?param=123,321,112
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=(.\,)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:0}" appendQueryString="false" />
</rule>
But whatever i try, it will only forwards the first param. leading to
/mypage?param=123
I have tried multiple combination of {C} and pattern regex.
IMPORTANT: before try something new remember ALWAYS to clear the cache of browser! it's mandatory.
Now...
the pattern you use is not correct, you have to use one suggested by Wiktor Stribiżew
So using pattern="myparam=([0-9\,]*) with url="/mypage?param={C:1}" try this:
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=([0-9\,]*)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:1}" appendQueryString="false" />
</rule>
If that is not correct there is something wrong in "," character... something about encoding?
if you try this one what happened?:
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=([0-9]*)\,([0-9]*)\,([0-9]*)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:1},{C:2},{C:3}" appendQueryString="false" />
</rule>
If only for mysite.com/?myparam=123,321,112, you can try the following URL rewriting rules:
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=(.*)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:1}" appendQueryString="false" />
</rule>

How redirect all web-site to other domain?

I have a test domain and it indexed in goolge, but it is wrong.
Now I need to create 301 redirect from all pages with all parameters to other domain.
I could to create action filter and create redirect on it, but it is wrong for me too.
I have known what it can implement in webconfig. But I don't understand how.
Do you help me please.
I try to use:
<rewrite>
<rules>
<rule name="Redirect to main subdomain from test domain">
<match url="^.*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)uh261477\.ukrdomen\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://kitesurfing-base.com{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it not work
You can place this rule in your web.config for this redirect:
<rule name="Redirect to main subdomain from test domain">
<match url="^.*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)testdomain\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://otherdomain.com{R:0}" redirectType="Permanent" />
</rule>