IIS URL Rewrite part of filename as folder - iis

I would like to use part of the filename as a folder name, my redirect is working but the rewrite does not work, this is my web.config (also has a rule to remove the file extension):
Original: https://www.ipressnet.com.br/v6/client_tracking.aspx?id=x
Rewrite: https://www.ipressnet.com.br/v6/client/tracking?id=x
THIS WORKS:
<rule name="RemoveASPx" enabled="true" stopProcessing="false">
<match url="(.*)\.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="AddASPx" enabled="true">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.aspx" />
</rule>
THIS DOES NOT WORK:
<rule name="RemoveClient" enabled="true" stopProcessing="false">
<match url="^v6/?$|^client_(.*)$" />
<action type="Redirect" url="client/{R:1}" />
</rule>
<rule name="AddClient" enabled="true">
<match url="^v6/client/(.*)$" negate="false" />
<action type="Rewrite" url="v6/client_{R:1}.aspx" />
</rule>
I appreciate any help, thank you!

You can write RemoveASPx and RemoveClient in the same rule.
The following rule maybe achieve your requirement.
<rule name="rewrite rule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/v6(.*)client_(.*)\.aspx$" />
</conditions>
<action type="Redirect" url="v6{C:1}client/{C:2}" redirectType="Temporary" />
</rule>
<rule name="(.*)" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/v6/client/(.*)" />
</conditions>
<action type="Rewrite" url="v6/client_{C:1}.aspx" />
</rule>
<rule name="r1" stopProcessing="true">
<match url="v6/(client)?(_)?(.*).aspx" ignoreCase="false" />
<action type="Rewrite" url="v6/{R:1}/{R:3}" />
</rule>

Related

IIS Redirect olddomain.com/en-us/ABC to newdomain.com/ABC

I'd like to put a redirect in web.config from olddomain.com/en-us/abc/blahblah to newdomain.com/abc/blahblah
Meaning in addition to the change of domain, the new URL doesn't have /en-us/, and everything else remain the same.
I picked up from here and there and came up with the following code, but it doesn't work. Could you help?
<rule name="Redirect New Site" enabled="true" stopProcessing="true">
<match url="^/en-us/(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{PATH_INFO}" pattern="^/abc/(.*)" />
<add input="{HTTP_HOST}" pattern="^olddomain.com/$" />
</conditions>
<action type="Redirect" url="https://newdomain.com/{R:1}" redirectType="Permanent" />
</rule>
For this requirement, I test it in my side. You can use the <rule> like below:
<rule name="redirectRule1" enabled="true" stopProcessing="true">
<match url="^en-us/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^olddomain$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/{R:1}" />
</rule>
Here is the rule and test result in my side. The <rule> in my web.config is:
<rule name="redirectRule1" enabled="true" stopProcessing="true">
<match url="^en-us/(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^localhost:49293$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/{R:1}" />
</rule>
Please note my old domain is localhost:49293.
Then when I request the url http://localhost:49293/en-us/Upload, it will redirect to new url http://newdomain.com/Upload(without en-us).
============================Update============================
<rule name="redirectRule1" enabled="true" stopProcessing="true">
<match url="^en-us/abc/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^olddomain$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/abc/{R:1}" />
</rule>

web.config redirect to www and redirect old pages to subdomain

I hope you can help me.
Originally I set up a rule to redirect all non-www traffic to the www site like this:
<rewrite>
<rules>
<rule name="Redirect https://example.com to https://www.example.com HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$"></add>
<add input="{HTTPS}" pattern="on"></add>
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>
</rules>
</rewrite>
Later on down the line we created a new site and moved the old site to https://business.example.com
When this happened, I was asked to create 301 redirects for the old blog posts to the new sub domain.
I created these rules:
<rewrite>
<rules>
<rule name="301 Redirect 1" stopProcessing="true">
<match url="live-better" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 2" stopProcessing="true">
<match url="a-zero-sum-game-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/a-zero-sum-game-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 3" stopProcessing="true">
<match url="buying-online-doesnt-mean-shopping-online" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/buying-online-doesnt-mean-shopping-online" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 4" stopProcessing="true">
<match url="the-truth-about-experiential-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/the-truth-about-experiential-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 5" stopProcessing="true">
<match url="winning-the-battle-for-brand-engagement" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/winning-the-battle-for-brand-engagement" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 6" stopProcessing="true">
<match url="experiential-technology-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/experiential-technology-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 7" stopProcessing="true">
<match url="what-retailers-have-forgotten" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/what-retailers-have-forgotten" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
When I initally tested this, it seemed to be working. But upon further investigation only the live-better path is redirected properly.
I changed my 301 redirects to this:
<rule name="301 redirects" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="ON" />
<add input="{PATH_INFO}" pattern="^/live-better" />
<add input="{PATH_INFO}" pattern="^/live-better/a-zero-sum-game-in-retail" />
<add input="{PATH_INFO}" pattern="^/live-better/buying-online-doesnt-mean-shopping-online" />
<add input="{PATH_INFO}" pattern="^/live-better/the-truth-about-experiential-retail" />
<add input="{PATH_INFO}" pattern="^/live-better/winning-the-battle-for-brand-engagement" />
<add input="{PATH_INFO}" pattern="^/live-better/experiential-technology-in-retail" />
<add input="{PATH_INFO}" pattern="^/live-better/what-retailers-have-forgotten" />
</conditions>
<action type="Redirect" url="https://business.example.com/{R:1}" redirectType="Permanent" />
</rule>
But it seems that now everything is redirected to https://business.example.com.
I thought it might be the original rule:
<rule name="Redirect https://example.com to https://www.example.com HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$"></add>
<add input="{HTTPS}" pattern="on"></add>
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>
If I am not mistaken, it will try to redirect https://anything.example.com to https://www.example.com (which might be why the redirects are not working).
Can anyone with more experience with this tell me what I am doing wrong?
I need the redirect from non-www to www, but at the same time I need the 301 redirects for the old blog posts.
Any help is massively appreciated.
I think I solved it.
It appears that order matters, so I changed the order so that "live-better" was last. I updated the non-www to www by adding the scheme like this:
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^situlive.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.situlive.com/{R:1}" />
</rule>
(thanks to this post: web.config redirect non-www to www)
so the full section now looks like this:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" />
</rule>
<rule name="301 Redirect 1" stopProcessing="true">
<match url="a-zero-sum-game-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/a-zero-sum-game-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 2" stopProcessing="true">
<match url="buying-online-doesnt-mean-shopping-online" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/buying-online-doesnt-mean-shopping-online" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 3" stopProcessing="true">
<match url="the-truth-about-experiential-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/the-truth-about-experiential-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 4" stopProcessing="true">
<match url="winning-the-battle-for-brand-engagement" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/winning-the-battle-for-brand-engagement" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 5" stopProcessing="true">
<match url="experiential-technology-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/experiential-technology-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 6" stopProcessing="true">
<match url="what-retailers-have-forgotten" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/what-retailers-have-forgotten" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 7" stopProcessing="true">
<match url="live-better" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better" redirectType="Permanent" />
</rule>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^main.js\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="main.js"/>
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

Redirect https www to https non-www in web api 2 azure website

In a Azure portal app, I configured traffic to redirect to https but https://www won't redirect to https://
Redirection from http://, http://www both work correctly.
Those are rules I have in web.config in azure app.
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="NonWwwRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(signalr)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(token)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
How can I achieve the needed redirect?
The problem was with DNS records. A record with host www was pointed to different IP. Changed it to same IP as # host, that solved the problem
How about:
<rewrite>
<rules>
<rule name="Redirect www OR non-https to https://" enabled="true" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAny>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
You could refer to the code as below:
<rule name="NonWwwRedirect" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:0}" redirectType="Permanent" />
</rule>
Note: Make sure the example.com hostname you have assigned to Site, so that you could reach it successfully.

Rewrite rule is not working proper in IIS for phpfox

**
I change my rewrite rule to change my website link from www.mydomain.com/index.php?do=/blog to www.mydomain.com/blog after this when i tried the below code, it won't let me change any page, like if i want to go for www.mydomain.com/blog, i just got stucked at www.mydomain.com, means redirect to my same root page,
what changes should i made for this please help. i am a newbei
my code is**
`<rule name="Redirect index.php" stopProcessing="true">
<match url="index\.php/(.*)" />
<action type="Redirect" url="{R:1}" appendQueryString="false" />
</rule>
<rule name="Rewrite index.php">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>`
Based on this forum post about .htaccess rewrites, you need to rewrite to:
index.php?do=/{R:1}
You are missing the ?do= part in the middle.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1">
<match url="^file/pic/photo/([0-9]+)/([0-9]+)/([A-Za-z0-9]{32}+)\-(.*?)_([0-9]*?)\.(.*)$" ignoreCase="false" />
<action type="Rewrite" url="file/pic/photo/{R:1}/{R:2}/{R:3}_{R:5}.{R:6}" />
</rule>
<rule name="Imported Rule 2">
<match url="^file/pic/photo/([0-9]+)/([0-9]+)/([A-Za-z0-9]{32}+)\-(.*?)\.(.*)$" ignoreCase="false" />
<action type="Rewrite" url="file/pic/photo/{R:1}/{R:2}/{R:3}.{R:5}" />
</rule>
<rule name="Imported Rule 3">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://www.%" />
</rule>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="[^/]$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{URL}/" />
</rule>
<rule name="Imported Rule 5">
<match url="^(.*)" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?do=/{R:1}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 6">
<match url="^file/pic/photo/(.*)\.(.*)$" ignoreCase="false" />
<action type="Rewrite" url="static/image.php?file={R:1}&ext={R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Multiple URL Rewrite rules getting conflicted

I have the following situation with url rewrite rules which are getting conflicted with each other:
Rule 1: I need to redirect my domain to https
Rule 2: I need to redirect www.mydomain.com --> https://mydomain.com
Rule 3: I need both www.mydomain.com and mydomain.com to redirect to https://mydomain.com/myfolder, but if I have mydomain.com/mysecondfolder should only redirct to https://mydomain.com/mysecondfolder
what I was able to achieve is everything but redirecting www.mydomain.com to https://mydomain.com (just because it is being conflicted with another rules, if alone it is working).
My rules are:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="redirect to myfolder" enabled="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/myfolder" />
</rule>
I was able to solve this using the below rules:
<rewrite>
<rules>
<rule name="Canonical Host Name" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^myapp\.com$" />
</conditions>
<action type="Redirect" url="http://myapp.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="redirect to items" enabled="false">
<match url="^$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/items" />
</rule>
</rules>
</rewrite>

Resources