Hide Azure Blob Url - blob

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."

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!

Serve specifics request in dedicated server for same domain

Asp.net Core App in IIS with its own domain served in two balanced servers.
Is it possible to have a specific controller attended in a third server only, but under the same domain?
Let's say http://MyApp.com/MyBusyController served in the third server and the rest of the app in the other two?
The reason for this is that this controller will be doing some heavy operations and it's going to get very busy, and don't want to degrade the other two servers, but have to be under the same domain.
You can use ARR load balance instead. So that URL rewrite rule can be used to manage the load balance.
https://learn.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/http-load-balancing-using-application-request-routing
You can create default inbound ARR_loadbalance rule and exclude the request to MyBusyController:
<rule name="ARR_MyWebFarm_loadbalance" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Rewrite" url="http://MyWebFarm/{R:0}" />
<conditions>
<add input="{REQUEST_URI}" pattern="MyBusyController" negate="true" />
</conditions>
</rule>
Then you can create reverse proxy rule for specific controller MyBusyController:
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing

ASP.Net Core 2.2 wwwroot static content hosted on S3 and CloudFront

I am using ASP.Net Core 2.2 and would like to host wwwroot static content on AWS S3 served by CloudFront. Haven't found anything on Google so posted the question here. I am familiar with AWS S3 and Cloudfront. I only need to find out how / what to change in ASP.Net Core razor application. Any advice and insight is appreciated.
I solved it by using rewrite rules in my server (IIS).
In order to do that you add a new IIS Url Rewrite module rule under system.webServer section in web.config.
Also, if you host your app in Apache/Nginx, there is a possibility to configure redirect rule based on configuration file with server-specific syntax.
Following is a sample rule, it redirects all resources starting with "media" to CloudFront distribution.
<rewrite>
<rules>
<rule name="Media to CDN" patternSyntax="Wildcard" stopProcessing="true">
<match url="media/*" />
<action type="Redirect" url="https://<my_cloudfront_id>.cloudfront.net/media/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>

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

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.

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.