I most likely might be missing something. I found this URL Rewrite example on this great post: http://marisks.net/2017/05/14/changing-static-resource-urls-to-cdn-urls-with-url-rewrite/
My setup is identical. I have relative URLs in my code to which I want to rewrite them on-the-fly to point to my CDN.
<outboundRules>
<clear />
<rule name="CDN" preCondition="CheckHTML" enabled="false" stopProcessing="true">
<match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*\.(jpg|jpeg|png|js|css|mp4).*)" />
<action type="Rewrite" value="http://cdn.example.com{R:1}" />
<conditions logicalGrouping="MatchAll">
</conditions>
</rule>
<preConditions>
<preCondition name="CheckHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
<customTags>
<tags name="Video">
<tag name="source" attribute="src" />
</tags>
</customTags>
</outboundRules>
This works great. Except I need to fine-tune the rewrite to specific folders.
I know this sounds simple, but when I look at the Regex in the above script - I fail to see where I can place any sort of restriction on the words specifically in my relative URLs. Obviously I need to work harder at Regex. But any help is appreciated.
I need to only capture relative links with certain folders like /media or /css ("/media/media.mp4" or "/css/base.css").
I believe I fixed the issue. At least, it properly works for the folders I've set to rewrite and ignores the folders I have not specified. I'll do some more testing, but this works for now.
<rule name="CDN" preCondition="CheckHTML" enabled="true" stopProcessing="true">
<match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(\/(media|css)+)*\/.*)" />
<action type="Rewrite" value="http://cdn.example.com{R:0}" />
<conditions logicalGrouping="MatchAll">
</conditions>
</rule>
The PreConditions and Tags are the same throughout.
Updated the Regex:
(^\/media\/.*)|(^\/css\/.*)
Related
I am trying to combine IIS URL Rewriting with routing in an ASP.Net 4.5 webforms site for subdomains and sub-directories.
I am trying to combine URL Rewriting in IIS with Routing in ASP.Net 4.5 for a webforms site. The solution I have right now works great for rewriting a subdomain with optional parameters to routing values, but I am struggling to pass sub-directory URLs to routes or querystrings. Most of the parameters I am working with are also optional so the rules have to consider any combination of those parameters or nothing.
The end goal is a feathering effect with hyphen separated locations on the subdomain side and categories on the sub-directory side. For instance a URL containing all parameters would be something like http://city-state.example.com/categoryslug/subcategoryslug
The rule I have in place in my web.config for the subdomains and the routes I have in my RouteConfig.cs are as follows:
<rewrite>
<rules>
<rule name="Rewrite subdomains">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.example\.net$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="{ToLower:/local/{C:3}/{C:1}}" />
</rule>
</rules>
<outboundRules>
<rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
<action type="Rewrite" value="http://example.net/{R:1}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
From RouteConfig.cs
// Geographic SubDomains
routes.MapPageRoute("local/state", "local/{state}", "~/local/Default.aspx");
routes.MapPageRoute("local/state/city", "local/{state}/{city}", "~/local/Default.aspx");
Potential Conflicting routes:
//Category
routes.MapPageRoute("category", "{catslug}", "~/category/Default.aspx");
//SubCategory
routes.MapPageRoute("category/subcategory", "{catslug}/{scatslug}", "~/category/Default.aspx");
The expected result in the end should rewrite something like http://city-state.example.com/categoryslug/subcategoryslug to ~/local/{state}/{city}/{categoryslug}/{subcategoryslug} but I see a potential conflict between that route and something like ~/local/{state}/{categoryslug} in which I don't think the application will be able to distinguish between state/category and state/city.
I also have some potentially conflicting routes for category/subcategory where the root domain displays categories using slugs. My concern there is that a route containing the same pattern of category/subcategory might end up being sent to the root domain instead of the subdomain.
Due to the aforementioned concerns I think using querystrings for the categories would be better than using routes. right now I can load a page at http://city-state.example.com/?category=categoryslug&subcategory=subcategoryslug just fine. Next I would like to be able to change those querystrings to categoryslug/subcategoryslug and rewrite it to /local/{state}/{city}/?categoryslug=categoryslug&subcategoryslug=subcategoryslug. I DO NOT want the url rewritten to anything like ?category=&subcategory= because that would not be compatible with how the pages select data based on querystrings.
<rule name="Rewrite subdomains">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.example\.com$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="{ToLower:/local/{C:3}{C:2}{C:1}{PATH_INFO}}" appendQueryString="true" />
</rule>
</rules>
<outboundRules>
<rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
<action type="Rewrite" value="http://example.com/{R:1}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
I have 2 .net API projects, servce1 and service2.
I would like to put them both under the same domain (site). I can ensure the APIs routing not overlapping.
I deployed the projects as:
Default Web Site/
service1 (raw url: http://mydomian/service1/api/ONE)
service2 (raw url: http://mydomian/service2/api/TWO)
What I want to achieve is that I can use http://mydomain/api/ONE or http://mydomain/api/TWO to access both service1 and service2 APIs without the subfolders in the URL.
I tried using URL rewrite module I got it works for one, but I can not get the second one work, with the same settings. See the setting file below.
I tried moving the rules orders. no luck.
<rewrite>
<rules>
<clear />
<rule name="first" stopProcessing="false">
<match url="(.*)ONE(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URL}" pattern="(.*)/api/ONE(.*)" />
</conditions>
<action type="Rewrite" url="/Servuce1/api/ONE{R:2}" logRewrittenUrl="true" />
</rule>
<rule name="seconds" stopProcessing="false">
<match url="(.*)TWO(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="(.*)/api/TWO(.*)" />
</conditions>
<action type="Rewrite" url="/service2/api/TWO{R:2}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
What is wrong with my settings?
What are the alternatives?
Should the stopProcessing be set to "true", so that the second rule is not applied when the first rule gets matched?
I have the following URL rewrite setup inside web.config working as I would like.
<rule name="Product Rewrite" stopProcessing="true">
<match url="^products/([^$]+)/([^$]+)" />
<action type="Rewrite" url="products?durl={R:1}&purl={R:2}" />
</rule>
Now I need to exclude any URLs that contain or end with /action/edit i.e. products/action/edit
I understand that I will need a conditions block, but I am not sure what to write.
Any help would be greatly appreciated.
Add a condition like below, negate true ensures it matches everything except action/edit
<conditions>
<add input="{URL}" negate="true" pattern="action/edit" />
</conditions>
I have a folder on my site for caching large flash movies and I want to stop other people from embedding them in their site; I would like to try and do this using the web.config file only. How could this be done?
My first attempt at a rule (which doesn't work):
The following rule was supposed to prevent public access (and embedding) to .swf files in the cache folder 'CurrentCache' - http://myurl.com/ContentCache/ and give a replacement movie 'NoEmbedFromCacheSWF.swf' instead.
<rule name="Prevent SWF hotlinking" enabled="true">
<match url="^(ContentCache)(.swf)$" ignoreCase="true" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^http://(.*\.)?myurl\.com/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/Content/Flash/NoEmbedFromCacheSWF.swf" />
</rule>
Thanks in advance!
Note: I think I have got the regex wrong in the <match url="A swf inside /ContentCache/" ignoreCase="true" /> line, any ideas what it should?
You can build an HttpModule for this. There is a blog posting describing exactly what you want to do I think:
HttpModule to block external referrers in ASP.NET
Edit: Of course I'm bending the rules here about web.config only. You have to use an external module, but then you can use it referencing from web.config only without modifying any of your code.
Edit2: If you want to do it using a rewrite rule, you have to change your pattern, like this:
<rule name="Prevent SWF hotlinking" enabled="true">
<match url="/ContentCache/.*\.swf$" ignoreCase="true" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^http://(.*\.)?myurl\.com/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/Content/Flash/NoEmbedFromCacheSWF.swf" />
</rule>
The pattern used is a regular expression, you can read up on them here and you can test them for example on this webpage.
I know the basics of how to rewrite but cant seem to find a rule that works the way I want it too.
Any help would be greatly appreciated :o)
<rule name="RedirectRule" stopProcessing="true" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?blah.com$" />
</conditions>
<action type="Rewrite" url="blog/{R:1}" />
</rule>
Basically I want it to only rewrite:
www.blah.com --> www.blah.com/blog
blah.com --> www.blah.com/blog
www.blah.com/blog --> do nothing.
www.blah.com/any-thing-else --> do nothing.
What about adding in a second condition to check PATH_INFO? Seeing as you only want to re-write when this is not specified...
<add input="{PATH_INFO}" pattern="^$" negate="true" />
The checks that PATH_INFO 'Does Not Match the Pattern' ^$ which is an empty string.
I'm not able to test this right now, so it might need tweaking...