IIS URL Rewrite to redirect all requests for a domain - iis-8

I have a web server that hosts multiple domains running IIS 8.5. One of those domains is going away and needs to be redirected to the root of another domain on the server. I created the rule below and it works fine as long as there is not path. If there is a path, it appends the path and results in a 404 error. I want to ignore the path and only redirect to www.DomainB.com. I would think path would only be included if I put in http://www.DomainB.com/{R:1} as the redirect url but this doesn't seem to be the case. Where am I going wrong here?
<rule name="Redirect DomainA.com">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*DomainA.com$" />
</conditions>
<action type="Redirect" url="http://www.DomainB.com/" redirectType="Found"/>
</rule>

I figured it out. The above is actually good. It was another rule for DomainB, one set to do a redirect from www to non www, which was causing the issue. It did not have DomainB in the condition, but rather a reg expression that matched any www and that rule does have the {R:1} on it, so the path was actually being appended by that rule after the DomainA.com rule processed. After making that rule DomainB specific, the above Redirect DomainA.com rule worked as is

Related

IIS hosted web application works fine with ssl without www, gives 404 error with www

Good morning.
I have a web application (made with angular if useful) hosted with IIS on a windows server 2016 with certificate (made with Let's Encrypts wacs) that works fine with ssl.
the problem is, if i use www the site not only appear as not safe, but also gives 404 error.
I tried the following rule on web.config to redirect to non www but without success
<rule name="Redirect WWW to non-WWW" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
I have other rules, thats my complete webconfig file: https://pastebin.com/wRWarfCV
I think https://www.example.com and https://example.com are Different domains. you need to reissue certificate and add http://example.com as primary domain and add other domain http://www.example.com.
More information you can refer to this link: How do I reissue my SSL certificate?.

.Net Core / IIS web config rewrite rules

In my situation I have two domains:
domain.com
domain1.com
where domain1.com is the alias of domain.com
For domain1.com I want to allow only traffic for the urls starting with api/ and redirect all other traffic to domain.com with 301 status code.
i.e. I want to allow requests like domain1.com/api/products/list on the other hand I want to redirect domain1.com/products/list to domain.com
Can anyone please guide how to achieve this with web.config?
Following URL redirect rule should work.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Domain1">
<match url="^(.+)" />
<conditions>
<add input="{HTTP_URL}" pattern="^/api/(.+)" negate="true" />
</conditions>
<action type="Redirect" url="http://domain/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Quick explanation.
We capture the whole incoming URL with match pattern for back-reference for the redirect if needed.
And in conditions we negate the API URLs pattern, hence the rule is applied to the URLs not containing /api.
The redirect action takes the captured back-reference and appends it to the domain.
Notes.
You might need to adjust slashes in the rule, I assume there will always be a slash after /api.
Browsers heavily cache permanent redirects, make sure to use a new Incognito/Private session each time you modify the rule.

Redirect old urls when changing to prefixless in Piranha Cms

I've unfortunately got a lot of pages SEO-indexed with "/home" as prefix.
Now I need to change to prefixless urls in Piranha, which is easy. But is there a place where I can force redirects from old urls to new urls?
E.g. redirect "www.example.com/home/page" to "www.example.com/page". The problem is that the old page doesn't exist anymore so I can't put a script on that page to do the redirect.
Best regards
Lars, Denmark
I think the best way is to use url rewrite module (need to install it from web platform).
In web.config You can append rewrite rules to system.webServer => rewrite => rules.
I will not test it, but it should be something like this:
<system.webserver>
<rewrite>
<rules>
<rule name="RedirectToPrefixless" stopProcessing="true">
<match url="^home/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>
Tag "match" finds for urls begining with "home/" - part of regex "(.*)" create group which will be used in redirect url
Tag "action" states, that for matching URL it should be redirected to "(.*)" part from "match" part.
So for example:
/home/test1 redirects to /test1
/home/test2?page=22 redirects to /test2?page=23
All redirects are "Permanent" (http status 301) - it can be also changed ;)

convert web.config rewrite rule to htaccess rewrite rule (All files without an extension to be processed as .cfm)

I am moving a site from IIS 7 to Apache 2.4 and have the following web.config rewrite rule I am having trouble converting to .htaccess. The rule essentially allows for clean (seo friendly) urls by rewriting all files without an extension with the .cfm extension (e.g. www.mydomain/bag rewrites on the server as www.mydomain.com/bag.cfm. The working rule in web.config is shown below
<rule name="Rewrite all non extension requests to .cfm" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.cfm" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{R:1}.cfm" />
</rule>
I have tried all the like posts I could find on here and unfortunately none of them worked for me. I am running Lucee 5.0 on CentOS 7 (Apache 2.4) if that matters. Any guidance would be greatly appreciated.
I don't really know about Tomcat; I assume you need Apache because Tomcat's no good for serving your non-script content.
You could set them up so that they both have the same document root, thus have congruent URLs, but have Tomcat listening on a different port, and not have that port open externally. Then you could proxy requests that are for (hidden) cfm files like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.cfm -f
RewriteCond %{THE_REQUEST} ^\S++\s++([^?\s]++)(\?\S*)?
RewriteRule ^ http://127.0.0.1:8080%1.cfm%2 [NS,NE,P]
RewriteRule (?<=.cfm)$ http://127.0.0.1:8080/404.cfm [NS,NE,P]
The second rule is an example to pretend that the cfm files are not there if request directly.
You will need the appropriate proxy module(s) enabled.

https://www.mydomain.com and https://mydomain.com go to different pages

Web Server: IIS 6.0 (ASP.Net 4.0)
I host two sites: one is the main web site and the other is the store site. Each are separate web sites in IIS. Each share the same wildcard SSL certificate. The store site uses a Host Header (store.mydomain.com) to direct traffic to it.
I want it so any URL used without the sub-domain "store" directs the user to the main web site, not the store web site.
The problem I'm experiencing is that the following URL always directs users to the default.aspx page on the store web site:
https://www.mydomain.com
Yet, these URLs correctly go to the main page on the main web site:
http://mydomain.com
http://www.mydomain.com
https://mydomain.com
What's up with the https://www that directs users to different page?
I have added a rewrite rule in the web.config file for both sites but it doesn't have any effect:
<rewrite>
<rules>
<rule name="Consistent Domain" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
First, are you sure this is IIS6? The URLRewrite feature is part of IIS7 and would have no effect under IIS6 if present. If this is IIS6 that may well be the answer to your question.
That aside, it is difficult to answer this based on the data given. It is possible that the bindings of the sites is incorrect and resulting in the traffic going to a site you don't expect.