URL Rewrite on Windows 2016 (IIS 10) with .Net Core to point to CDN - asp.net-core

I have a brand new virtual server running Windows 2016 (IIS 10). I have a website that is built with .NET Core 1.1.
I would like to rewrite all image, js, font, and css references to point to a CDN.
Problem 1, adding this rule:
<rule name="CDN Assets" stopProcessing="true">
<match url="^css/(.+)$|^images/(.+)$|^fonts/(.+)$|^js/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="https://cdn.example.net/{R:0}" appendQueryString="true" />
</rule>
... results in a 404 for every request. For example a request to https://example.net/images/en-us/logo.png even though the image exists both in the real folder under and at https://cdn.example.net/images/en-us/logo.png
Is this possibly caused by .NET Core or by HTTP2 (which is introduced in IIS 10). Or do I need some sort of outbound rule also?
Problem 2, adding an outbound rule like:
<rule name="Outbound CDN: images" preCondition="IsHtml">
<match filterByTags="A, Img, Link" pattern="^/images/(.+)$" />
<action type="Rewrite" value="//cdn.example.net{R:0}" />
</rule>
<preConditions>
<preCondition name="IsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/>
</preCondition>
</preConditions>
Causes the page response to become complete mangled unreadable garbage. Is this because of the response is sent over HTTPS?
UPDATE (2016/03/16 8:01PM MST)
Using #EricB suggestion I have tried implementing it via the Microsoft.ASPNetCore.Rewrite package to no avail.
I have this in my Startup.cs and just the local images are being loaded:
string CDNroot = Configuration["URLs:CDN"].Trim();
RewriteOptions rewriteOptions = new RewriteOptions()
.AddRewrite(#"^(css|images|fonts|js)(.+)$", CDNroot + "/$1$2", skipRemainingRules: true);
app.UseRewriter(rewriteOptions);
If I change this to:
string CDNroot = Configuration["URLs:CDN"].Trim();
RewriteOptions rewriteOptions = new RewriteOptions()
.AddRedirect(#"^(css|images|fonts|js)(.+)$", CDNroot + "/$1$2");
app.UseRewriter(rewriteOptions);
... and request an image URL it is correctly redirected to the CDN URL. But you wouldn't want a redirect, as that is adding an extra 30x response to every asset request. I am stumped why Redirect works, but Rewrite doesn't.

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!

Hide Azure Blob Url

I have a large amount of files stored in a public Azure blob container, all of which are referenced directly via the HTML in my ASP.NET MVC web application. As an example a path to one of the images in blob storage looks like so:
//<my-storage-account-name>.blob.core.windows.net/public/logo.png
I want to avoid displaying my storage account name in my HTML source code so rather than:
<img src="//<my-storage-account-name>.blob.core.windows.net/public/logo.png"/>
I'd prefer to use this:
<img src="/images/logo.png"/>
I want to avoid setting up an MVC route and using the blob API to load the file into the response stream so thought a web.config solution might be the simplest solution, i.e.
<rule name="Image Redirect" stopProcessing="true">
<match url="^images/(.*)$" ignoreCase="false" />
<action type="Redirect" url="//<my-storage-account-name>.blob.core.windows.net/public/{R:1}" redirectType="Permanent" />
</rule>
QUESTION: Is this the most efficient method given that any page could be loading 30+ images at a time? Or should I just use the public blob URL despite my concerns for performance gains?
I found a Microsoft Hands-on Lab where they recommend the web.config URL rewrite rule option:
Hands on Lab: Maintainable Azure Websites: Managing Change and Scale (July 16, 2014)
(Code Snippet - WebSitesInProduction - Ex4 - UrlRewriteRule)
<system.webServer>
<rewrite>
<rules>
<rule name="redirect-images" stopProcessing="true">
<match url="img/(.*)"/>
<action type="Redirect" url="http://[YOUR-STORAGE-ACCOUNT].blob.core.windows.net/images/{R:1}"></action>
</rule>
</rules>
</rewrite>
"Note: URL rewriting is the process of intercepting an incoming Web request and redirecting the request to a different resource. The URL rewriting rules tells the rewriting engine when a request needs to be redirected, and where should they be redirected. A rewriting rule is composed of two strings: the pattern to look for in the requested URL (usually, using regular expressions), and the string to replace the pattern with, if found. For more information, see URL Rewriting in ASP.NET."

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>

IIS URL Rewrite rule not being triggered

I am pretty sure that I have my WCF URL Rewrite Rule coded correctly in my Web.config file:
<rewrite>
<rules>
<rule name="RemoveSVC" stopProcessing="true">
<match url="MyWebSvc/([a-zA-Z]+)-svc/(.*)" />
<action type="Rewrite" url="MyWebSvc/{R:1}.svc/{R:2}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
The rewrite rule seems to be working in the "Test pattern..." widget. For instance:
http://localhost/MyWebSvc/thinga-svc/majigs
gets correctly mapped to:
{R-1} thinga
{R-2} majigs
And I would like the URL to be re-written to:
http://localhost/MyWebSvc/thinga.svc/majigs
Seems simple enough, however, when I try to test the URL from a browser:
http://localhost/MyWebSvc/thinga.svc/majigs
works, but
http://localhost/MyWebSvc/thinga-svc/majigs
does not appear to be re-written, and I get a 404 response code. Do I have to configure the rule to be triggered with some other setting?

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.