Asp.net url rewrite for image file extensions - asp.net

I'm replacing all the images in our website with .webp
Basically we had .jpeg images and now replaced with .webp images.
But we're seeing many images in google especially.
And to prevent 404 errors I'm trying to find a way to redirect to webp images.
We're using asp.net rewrite in web.config file.
But couldn't figure out what I'm missing.
sample old image link: https://www.example.com/SiteThumbs/g/google.com.jpeg
and the new image link: https://www.example.com/SiteThumbs/g/google.com.webp
Just the extension is different now.
And the rule in web.config file:
<rule name="Old Images" stopProcessing="true">
<match url="^SiteThumbs/(.*).jpeg" ignoreCase="true" />
<action type="Rewrite" url="SiteThumbs/{R:1}.webp" appendQueryString="true" />
</rule>
Could anyone advice please.

This rule is working actually.
It was Cloudflare caching the images so never hits the server.

Related

web.config redirect not working on localhost

I have a website that I work on in visual studio and then deploy to azure. I'd like to create a permanent redirect in my web.config file. I would like to test this to make sure it works before I deploy to azure.
Here is my web.config file
<rewrite>
<rules>
<rule name="301Redirect" stopProcessing="true">
<match url=".*/PEOs.aspx" />
<action type="Redirect" url="http://myblogpost.com" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
So when the user visits www.mysite.com/PEOs.aspx they will be directed to my blog post. The problem is, when I test this, it just goes to the page. Doesn't redirect. I realize I could just write some code in the code behind file, but I want something that's more SEO friendly.
Am I doing something wrong here? I'm fairly certain regex can be used here. But when I replaced it with http://localhost:21537/PEOs.aspx it doesn't work either. I don't want to publish to azure unless I know that this will only be an issue during local testing.
Try using: <match url="PEOs.aspx" />
I was not entirely sure so I checked it here: https://support.rackspace.com/how-to/rewrite-urls-from-aspnet-on-cloud-sites/

Route all requests for files to a simple folder name for serving PDFs

I would like to make a small URL for all files in a folder. So instead of writing:
http://mywebsite.com/content/pdf/samples/Myfile.pdf
I can write:
http://MyWebsite.com/samples/myfile.pdf
I have added
routes.MapPageRoute("Sample", "Sample/{*fullpath}", "~/Content/pdf/samples/{*fullpath}", False)
which does seem to redirect, but I get There is no build provider registered for the extension '.pdf'. However, it works fine when I put an ASPX file in the folder and navigate to it. So then, I add:
<add extension=".pdf" type="System.Web.Compilation.PageBuildProvider" />
Which now gives me a parser error because the page is filled with gibberish.
Same thing happens for images.
Is what I am asking not doable? It seems that there should be a way to have it understand to serve or download the page as it would without the routing.
Is there a better way?
Well, I found something that works, though I don't know how efficient or great it is.
I kept
routes.MapPageRoute("Sample", "Sample/{*fullpath}", "~/Content/pdf/samples/{*fullpath}", False)
Then I added the following to the Web.Config.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect PDFs" stopProcessing="true">
<match url="^sample/?(.*)$" />
<action type="Redirect" url="/Content/SampleReports/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
I would still love to know if there is a better, faster, or easier way to do this. Also, this works for PDF files. I assume I would need to make a rule for each type of static file I want to serve from the sample directory.

IIS Reverse Proxy

I've got two websites.
XX.XX.XX.XXX:5917 which is a webforms website
And
XX.XX.XX.XXX:5916 which is an mvc website.
Both websites on the same IIS 7 server. I can navigate each website, login, etc.
However, when a user goes to XX.XX.XX.XXX:5917/Report I want the content from XX.XX.XX.XXX:5916/Report to be served up, but the url to remain XX.XX.XX.XXX:5917/Report.
To do this, I'm trying to set up a reverse proxy on the 5917 site to serve up content from 5916.
When I have a redirect rule in place, I can click a link in 5917 to Reports and it will take me to 5916/Reports. This works, but changes the address bar. When I use the Rewrite rule option, absolutely nothing discernible happens. If I screw up the end url in the action bracket then the page will break, so I know it's at least evaluating the rule.
Here is the 'working' redirect rule:
<rule name="Reverse Proxy to Reports" stopProcessing="true">
<match url="\bReport\b" />
<action type="Redirect" url="http://XX.XX.XX.XXX:5916/{R:0}" />
</rule>
Am I missing anything? Where do I go from here?
Try adding this on your XX.XX.XX.XXX:5917 WebForms web.config:
<system.webServer>
...
<rewrite>
...
<rules>
<rule name="ReverseProxyInboundRuleForReports" stopProcessing="true">
<match url="(Report/.*)" />
<action type="Rewrite" url="http://localhost:5916/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
You might need 'Application Request Routing' extension on IIS for it to work though. If so then you can get it from this link:
http://www.iis.net/downloads/microsoft/application-request-routing
You can also read more about the technique on this link:
http://weblogs.asp.net/owscott/creating-a-reverse-proxy-with-url-rewrite-for-iis
Good luck!

Specifying precompressed (gzip) vs non-precompressed content in IIS7 by filetype

I am trying to essentially setup an IIS7 intranet CDN for a series of nested folders containing some precompressed (gzip) css content but also image files (obviously noncompressed).
The folder structure is essentially something like this:
Skins
Skins\SkinName1\*.css
Skins\SkinName1\Control1\*.png
Skins\SkinName1\Control2\*.png
Skins\SkinName1\lots more controls..\*.png
Skins\SkinName2\...
Skins\lots more skins...\...
..you get the idea..
The css is all pre-compressed (gzip'ed) so I need to set a Content-Encoding header of 'gzip' on them.. but I obviously can't set the same header on the image files.
I know I could set the Content-Encoding gzip folder at the root, and then not-inherit it on all the Control (image) folders.. but there's a ton of image folders to have to go through.
Is there any way to set this by file type or something that would keep from having to go through each folder individually?
*Unfortunately I don't have any control over the folder structure.
Thanks-
OK, I finally realized I should be using the URL Rewrite module and let it add the custom header.
I basically just used this:
<rewrite>
<outboundRules>
<rule name="Set custom HTTP response header" enabled="true">
<match serverVariable="RESPONSE_Content_Encoding" pattern=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="\.css$" />
</conditions>
<action type="Rewrite" value="gzip"/>
</rule>
</outboundRules>
</rewrite>
and let the non-precompressed stuff default as it should.
hope it helps someone-

How do I rewrite / to /blog in IIS 7?

I'm having trouble constructing the correct rewrite rule.
Here's what I need the rule to do:
http://www.mydomain.com/this-is-my-page
http://www.mydomain.com/blog/this-is-my-page
Do you want to rewrite or redirect?
If all you want is redirect, then it is really easy:
<rewrite>
<rules>
<rule name="Blog Rule" stopProcessing="true">
<match url="^this-is-my-page$" />
<action type="Redirect" url="/blog/this-is-my-page" />
</rule>
</rules>
</rewrite>
However, if you want to Rewrite, then you will need to make sure that all links, images, styles, scripts, etc, are linked using the site absolute path (/some-link/ rather than some-link/) or otherwise you are going to have a lot of broken links and styles.
You can use URL Rewrite to fix them using Output Rewrite but that is more complicated to get right.
I have a sample that shows how to do some of the output rewrite here: Link

Resources