Hi, on my dev server I want to show, local images if they exist, and if they do not exist I want to fetch them from our live server. Right now, all broken images on my local causes redirect loops. And whatever i put in the rewrite the loop still takes place. How can I break the loop and serve a different image?
The problem exist regardless, but this is the reverse proxy i have tryed.
If the error is a permanent redirect it will be cached by the browser and you probably is looping between two cached urls.
When I test different sites I usual use the CDN feature in litium, then all storage-links will always be pointed to the live site and I don’t need to storage folder on my local computer.
I use the cdn option most of the time, but sometimes we want to test content that dose not exist live, only on the test server, so we want to be able to only fetch external images if the file is a 404 locally.
The rule should work fine, but we get this redirect loop before we even hit the rule it seams?
The loop is not a cache problem as far as i can tell, you can try your self, our test server dose not have the logo locally:
No logs, this only happens on images that have the url in the database but missing files, random storage url returns 404 as expected. I think you can recreate this on a fresh install. We have seen this behavior for a while.
Regardless of this redirect loop, if I add my rule higher up in the hierarchy it migt bypass it? Can i do that somehow?
Also one more reason we need to do this, is that there is a “bug” in the backoffice when using the cdn option. the preview of the blocks cant be viewed/generated while using cdn do to “canvas tainted” exeptions.
Edit, so am now guessing that even with this fix since we dont know the file location on the server matchType=“IsFile” negate=“true” will not work?
Not sure exactly where and how the url-rewrite-rules attaches into the application flow. Maybe you can attach to the EndRequest event (global.asax) and there check if status is 404 and url starts with storage and in that case change the response to a redirect instead to the origin domain…
After adding a protocol to “return string.Concat(_protocol + _host, mediaUrl)” it worked wonders thanks!.
But will the blob check slow down production? Should I do some checks if in producion or can I “disable” the GetLocation() decerator in the constructor based on settings?
public override string GetLocation<T>([NotNull] MediaLocationServiceArgs args)
{
var mediaUrl = _parent.GetLocation<T>(args);
if(_isProduction){
return mediaUrl;
}
var blob = _blobService.Get(args.BlobUri);
if (string.IsNullOrEmpty(_host) || blob?.GetDefault()?.Exists == true)
{
return mediaUrl;
}
return string.Concat(_host, mediaUrl);
}
The var blob = _blobService.Get(args.BlobUri); is only in memory but the blob?.GetDefault()?.Exists == true will check that the actual file exists.
I think you in the most cases compiling the solution in DEBUG mode when running local and RELEASE mode for production. In that case I should have used conditional code compilation.
#if DEBUG
/// all code that only should be there during debug build, in this case the full class
#endif