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

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.

Related

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>

Building IIS Redirect Rule to Redirect Product Page to New URL Passing Product ID

I'm attempting to use the URL Rules engine in IIS 7 to redirect our current product pages to our new product pages.
Old Pages:
example.com/itemform.aspx?item=X12878
example.com/itemform.aspx?item=Y87304&showmenu=T
New URL
example.com/c/product/X12878
example.com/c/product/Y87304
Attempt
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([.a-zA-Z0-9]+)$" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
Complete Rewrite Rule w/ Suggestion by Emma
<rewrite>
<rules>
<rule name="Product Page Redirect" stopProcessing="true">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<conditions>
</conditions>
<action type="Redirect" url="https://newsite.com/x/product/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Accoridng to your description, if you want to use url rewrite to get the right query string.
More details, you could refer to below codes:
<rule name="QueryStringRue" enabled="true" stopProcessing="true">
<match url="itemform.aspx" />
<conditions>
<add input="{QUERY_STRING}" pattern="^item=([0-9a-zA-Z]*)" />
</conditions>
<action type="Redirect" url="http://example.com/c/product/{C:1}" appendQueryString="false" />
</rule>
Result:
My guess is that maybe removing the end $ anchor might simply make the match valid:
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
but, maybe not.

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>

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>