.Net Core / IIS web config rewrite rules - asp.net-core

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.

Related

Unable to convert IIS web.config URL Friendly to .htaccess mod_rewrite

I'm migrating a web application from a Windows (IIS) server to a Linux (Apache) and I have been a few days trying to adapt the application, configs, etc. for Linux/Apache.
Everyting is working except the url friendly I was using with Windows/IIS. And I don't know if this is actually a URL Friendly, a simple redirect or anything else.
This is what I want:
I have a 'app.php' file which is like the main application 'index', and shows different 'pages' depending on the old/ugly url parameters:
https://example.com/app.php?qpage=5&qsubpage=2&action=new
Instead of writting this, I want to use friendly urls like this:
https://example.com/section5/?qsubpage=2&action=new
(Please notice that the second and third parameters 'qsubpage' and 'action' are optional!)
Ok, with IIS and this lines in web.config, it works fine:
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="app.php?qpage={R:1}" />
</rule>
</rules>
</rewrite>
Then, in 'app.php' file I get the current URI and (optional) parameters to show one content or another.
However, I can't reach to get it working with .htaccess.
This is what I have know. It works fine only if there is just one parameter:
If I go to...
https://example.com/section5
...it shows the correct 'page' correctly.
However, if I go to...
https://example.com/section5/?qsubpage=2
or
https://example.com/section5?qsubpage=2
...'qsubpage' is ignored.
If I go to
https://example.com/section5&qsubpage=2
...I get a '404: url not found' error (however it works in Windows)
This is my .htaccess rule:
RewriteEngine On
RewriteRule ^([A-Z-a-z-0-9]+)/?$ simplygest.php?qpage=$1&qsubpage=$2 [L]
I have tried some online mod_rewrite generators and even web.config to .htaccess converters online with no success.
Thanks!

IIS Rewrite very confusing

concerning IIS Rewrites.
I want to change this url on my local IIS Instance
http://localhost/MySite/health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c
to redirect to this url:
http://localhost/MySite/Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c
As you can see I don't want the incoming request to use
Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c
instead to use
health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c
The Health.svc is the WCF endpoint name, so I just want /health with the key and filter parameter at the end as shown.
Whatever I put in my web config rewrite it still doesn't work. I am rather confused what bit of the url to put in, as the regex seems to be valid as I can test it in IIS and online regex validators.
<rewrite>
<rules>
<rule name="HealthRewrite" stopProcessing="true" enabled="true">
<match url="MySite\/health\?key=([0-9a-zA-Z=]+)&([a-z])" />
<action type="Rewrite" url="MySite/Health.svc/{R:1}/{R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
How can I get this to work? I have got the rewrite module installed as can see it in IIS an also can see the dll is registered.
If you want to match the value of the query string in IIS, you need to use {QUERY_STRING}. Here is a demo:
<rewrite>
<rules>
<rule name="Test">
<match url="(Service1)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(key)=(.*)" />
</conditions>
<action type="Rewrite" url="Service1.svc/{C:2}" />
</rule>
</rules>
</rewrite>
This is my web.config.
This URL:
http://localhost/Service1?key=getdata
will redirect to this url:
http://localhost/Service1.svc/GetData
For your last question, why add the MySite prefix? This is because the URL in the Rewrite URL will be used as the redirect URL. Notice that it uses back-references to preserve and rearrange the original URL pieces captured during pattern match. For Rewrite, all prefixes other than localhost must be provided in the Rewrite URL.
I managed to get it working with going to all sorts of sites as its not obvious at all.
They key seemed to put the conditions in and then a {QUERY_STRING} with regex which can then create the {C:1} and {C:2} groups that are pushed into the new rewrite
<rewrite>
<rules>
<rule name="HealthRewrite" stopProcessing="true" enabled="true">
<match url="^health" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="key=([0-9a-zA-Z=]+)&([a-z])" />
</conditions>
<action type="Rewrite" url="/MySite/Health.svc/{C:1}/{C:2}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
I found it confusing knowing what url to match but with a few simplified tests of just ^health I could see more easily and play around with getting the query string parameters. I had to provide the Rewrite with the /MySite/ prefix which is confusing as the match didn't need that!

IIS URL Rewrite to redirect all requests for a domain

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

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 ;)

301 redirect on IIS. Redirect non-www to www

I ought to redirect my website from non-www to www on IIS 6. e.g if I enter domain.com in the url, it has to redirect to www.domain.com for SEO optimization.
I followed the video "http://www.youtube.com/watch?v=PYxabNrIMQ4" for creating my rewrite rule. And I made it as below
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
But it is not working as expected. Help me out to find the solution.
Note:
My Visual studio shows warning like "The element 'system.webServer' has invalid child element 'rewrite'".
I believe it doesn't make any problem.
According to this video you can redirect using the IIS Manager UI. Make sure you have a second site for the non-WWW version, with the same "Location" as the WWW version of your site; when creating it, be sure to specify your non-WWW domain for the "Host Header" field.
After creating it, right-click the site and go to Properties. Disable logging if you're so inclined (probably not needed because you'll be redirecting anyways), then go to the "Home Directory" tab. Now remove the Application by clicking the appropriate "Remove" button, since it's not used for redirection. Next, select the "A redirection to a URL" radio button, be sure to check "The exact URL entered above" and "A permanent redirection for this resource".
Lastly, in the "Redirect to" field, enter your full WWW url followed by $S$Q (i.e. http://www.example.com$S$Q) -- the $S$Q are important to capture any path and query string the client might send, so if the user goes to http://example.com/foo/bar.html they will be appropriately redirected to http://www.example.com/foo/bar.html instead of just http://www.example.com/ . Click Apply and OK, and your redirection should be set!
Does this works ? remember to add in the root web.config
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>