IIS rewrite rule one query parameter with multiple values - regex

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>

Related

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 rewriting of IIS

with some hit and trail, i managed to remove the extention of the pages which i view for my website, now i want to replace all the ? and = and the & signs with backslash /, I am really Lost now, how do i write that rule in my web config
My Web.Config File:
<rewrite>
<rules>
<rule name="Redirect .cfm extension" stopProcessing="false">
<match url="^(.*).cfm$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).cfm$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .cfm extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.cfm" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.cfm" />
</rule>
</rules>
</rewrite>
with the above, i managed to get a page like this
http://website.ca/graphics?mode=Graphics%20Design%20%C2%BB%20Business%20cards&catID=35&section=graphics
the graphics? is the one which was before graphics.cfm?

IIS redirect with regex [duplicate]

Whenever someone makes request over HTTP protocol I rewrite the url to make it HTTPS. This is the code in web.config:
<rule name="Imported Rule 1-1" enabled="true" stopProcessing="true">
<match url="^(?!https://).*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="80" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="https://abc.com/{R:1}" />
</rule>
However when I browse on http:// I get IIS error
HTTP Error 500.50 - URL Rewrite Module Error. The expression "https://abc.com/{R:1}" cannot be expanded.
How can I resolve this? I am utterly confused.
The matches are zero based.
<action type="Rewrite" url="https://abc.com/{R:1}" />
Won't work because you only have one match. You need:
<action type="Rewrite" url="https://abc.com/{R:0}" />
Also, this won't work, because you can only match on the path below the site root.
<match url="^(?!https://).*" ignoreCase="false" />
It looks like you are checking for ssl. Try this instead:
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
You can redirect through web config to
Hope it will help full
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^abc.com$" />
</conditions>
<action type="Redirect" url="http://www.abc.com/{R:0}" redirectType="Permanent" />
</rule>

Exclude path in IIS rewrite rule?

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>

IIS7 URL Rerwrite - how to replace all underscores with hyphens in a Regex?

I am using the URL Rewrite feature in IIS7 to turn the URL:
/main.asp?category=Name_Of_A_Product
Into:
/category/name-of-a-product/
I have created the redirect & rewrite rules below which do the majority of the work, except I cannot find a way of replacing the underscores with hyphens.
Each URL can have between zero and many underscores and I'm trying to replace them in a single regular expression, to avoid chains of 301 redirects (as I believe that is bad for SEO).
Do you know how (or if) this is can be done?
<rule name="Redirect REAL to FRIEDNLY" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([^=&]+)($|&(.*))$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}/" appendQueryString="false" />
</rule>
<rule name="Rewrite FRIEDNLY to REAL" stopProcessing="false">
<match url="^category/([^/]+)/?$" />
<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="main.asp?category={R:1}" />
</rule>
Unfortunately IIS7 has a few limitations:
you can only capture 9 groups C:1 ... C:9
there is only one string function and that's ToLower
Because of that you'll be limited to a URL with a maximum of 9 words separated by max 8 underscores (eg. /main.asp?category=One_Two_Three_Four_Five_Six_Seven_Eight_Nine) and you'll be forced to use 9 rewrite rules:
Single Word: /main.asp?category=Product
<rule name="Redirect REAL to FRIEDNLY 1" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}/" appendQueryString="false" />
</rule>
Two Words: /main.asp?category=Some_Product
<rule name="Redirect REAL to FRIEDNLY 2" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)_([A-Za-z]+)$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}-{ToLower:{C:2}}/" appendQueryString="false" />
</rule>
Three Words: /main.asp?category=Some_New_Product
<rule name="Redirect REAL to FRIEDNLY 3" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)_([A-Za-z]+)_([A-Za-z]+)$" />
</conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}-{ToLower:{C:2}}-{ToLower:{C:3}}/" appendQueryString="false" />
</rule>
            ...            ...            ...            ...            ...            ...