IIS URL Rewrite Module - Regular Expression for URL - regex

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>

Related

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>

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>

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>