Stopping hotlinking from a directory using web.config - asp.net

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.

Related

NGINX - How to deny direct access to a file

Is there any way to prevent direct access to a file but allow it to be loaded from another page? For example, I have an swf in mydomain.com/assets/importantasset.swf and I want to load it in mydomain.com/page, but if people try to directly access that swf they will be shown a 403 error. I know there is a way to accomplish this in IIS, if anyone can convert the following code to be compatible for nginx.
<rule name="protect" stopProcessing="true">
<match url=".*\.(swf)$" negate="false" />
<conditions>
<add input="(HTTP_REFERER}" pattern="^http://domainname.com/.*$" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>

Using IIS Rewrite URL for SEO to move all Cold Fusion CFM pages to ASP aspx pages

I am moving a large database driven website from coldfusion .cfm to .net aspx. I am pretty much finished now, but I need to do 301 redirects on all the coldfusion pages to the new aspx ones, so google and such like as we don't want to lose the search engine positioning. So I intended to use URL Rewrite for this, but I cannot get it working, most of the time I just get 404s back.
Basically, my new aspx pages are all the same filename, just .cfm is replaced with .aspx, some pages can have a long querystring after them and some not.
Examples:
http://www.example.com/test.cfm needs to be remapped to http://www.example.com/test.aspx
http://www.example.com/test2.cfm?a=1&b=2&c=3 needs to be remapped to http://www.example.com/test2.aspx?a=1&b=2&c=3
The site itself has hundreds of pages and some pages have over 8 variables in the querystring, so I just wanted to try and do a straight map over in URL rewrite. I cannot do a rule per page as that will take ages!
My current attempt is:
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="^http://www.example.com/(.*).cfm(.*)$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{C:1}.aspx" appendQueryString="true" logRewrittenUrl="true" />
</rule>
This just does a 404 for me. Obviously the cfm pages do not exist and the aspx ones now do. I still have cold fusion installed on the server for now, and do not want to uninstall it until google has updated itself (it case of issues so I can always go back).
Any help would be much appreciated.
Thanks
David
It's like this...
<rule name="CFM301ASP" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="^.*\.cfm$" negate="false" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
</conditions>
<action type="Redirect" url="/yourfile.aspx" appendQueryString="true" redirectType="Permanent" />
</rule>
This only works on CFMs that do not exist (isFile=false). It will 301 redirect CFMs that would cause a 404, to your ASP file, and append the URL variables.
The documentation seems to cover all of this.
From the docs on HTTP Redirect:
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*.php" destination="/default.htm" />
</httpRedirect>
</system.webServer>
</configuration>
This led me to believe that you should be able to map URLs from one file extenstion to another.
Creating Rewrite Rules for the URL Rewrite Module
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
Try reading through these docs, you should be able to find the correct syntax that will allow you to just transfer from *.cfm to *.aspx, passing along the same query strings, or making translations as needed.

IIS URL Rewriting and MVC

My hosting plan has a limited number of web applications for use, but unlimited subdomains. I plan to take advantage of these subdomains by using IIS rewriting, like the following:
<rule name="Home Rewrite" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^home\.mydomain\.com$" />
</conditions>
<action type="Rewrite" url="home/{R:1}" />
</rule>
This works fine for the most part, I can go to http://home.mydomain.com and it'll take me to what is essentially http://www.mydomain.com/home as expected.
I am publishing using Web Deploy, and I believe the host is IIS7.
The problem is that I want to take advantage of #Html.ActionLink, but when viewing the source, this resolves out to include the virtual directory.
So what I end up with is a site that works when I go to the original address:
http://www.mydomain.com/home/application
And a site that loads, but doesn't function correctly, at the redirected address:
http://home.mydomain.com/application
With generated URLs in the page source pointing relative to the original address:
/home/application/Account/Login
This applies to links to other pages/routes, bundles, basically anywhere that ~/ or #Html.ActionLink is used.
How do I get around this? I'm hoping to keep the use of #Html.ActionLink at least, I think I can live without the tildes.
I finally found a solution!
https://support.gearhost.com/entries/23689272-URL-Rewrite-Subdomain
My web.config rewrite rule required an extra line:
<rule name="Home Rewrite" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^home\.mydomain\.com$" />
<add input="{PATH_INFO}" pattern="^/home/" negate="true" /> <!-- This one! -->
</conditions>
<action type="Rewrite" url="home/{R:1}" />
</rule>
Now everyone is happy :)
You could write a custom HtmlHelper so instead of using ActionLink you can use MyActionLink and it can generate the url you need.

MVC3 + WordPress IIS Url Rewriting Rules

I have a ASP.NET MVC3 website located at http://mydomain.com/mymvcapp/. However, the root of the webiste (mydomain.com) contains a WordPress site running PHP. Therefore, I put the following IIS URL Rewrite rule to allow WordPress to function correctly via its rewriting mechanisms:
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php"/>
</rule>
</rules>
</rewrite>
So with this rule in place my WordPress functions perfectly, however, my MVC rewriting does NOT work. How would I alter this rule to allow both WordPress and MVC (under the /mymvcapp/ folder) to coexist nicely?
Figured it out on my own. Regex is probably one of the most powerful YET complicated / confusing technologies there is. But in this case the patternSyntax flag was set to Wildcard, not Regex, which caused my confusion. Hope this helps someone else out there! =]
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{PATH_INFO}" pattern="/mymvcapp/*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
This is one of the very few posts anywhere that talks about making WordPress and ASP.NET coexist nicely in an IIS setup. So Kudos for that.
I was thinking to post this as comment to either your original question or the answer, but I chose to write an "answer" only because this is an honest question and I need some formatting capabilities.
I have multiple ASP.NET apps running on by site. In particular the root website is running an MVC4 app.
Since I cannot have WordPress installed at the root, my plan was to have it on its own app folder http://mydomain.com/wordpress/ and then have a URL-rewrite rule that to do the following (using peudo-code):
blog.mydomain.com/{path} --> mydomain.com/wordpress/{path}
I've only caused a mess with this approach and have not been successful using pretty permalinks, sometimes getting into redirect-loops and other times breaking links to .css files, admin pages, etc...
Have you ever given this a thought, i.e., having wordpress as a subapp instead and do sub-domain URL-rewriting?!?!
I had a similar situation but I had no need to edit my web.config file. Instead I followed instructions here at https://wordpress.org/support/article/giving-wordpress-its-own-directory/ where this is documented.
At point 7) within Moving WordPress process to a subfolder Method II (With URL change) you find options for a IIS installation.

Using IIS7's Rewrite Module and a database

My company converted from an old website to a new one and we have a bunch of old pages with URLs like this:
www.example.com?foo.aspx
www.example.com?foo.aspx?ID=B&utm_source=Foo
www.example.com?foo.aspx?ID=C&utm_source=Foo
Those URLs need to go to these pages respectively:
www.example.com/ProductA
www.example.com/ProductB?utm_source=Foo
www.example.com/ProductC?utm_source=Foo
I can get this to work by using in my web.config but there are so many I would prefer to do it in the database. I have been able to partially successfully switch to the database using the article http://learn.iis.net/page.aspx/803/using-custom-rewrite-providers-with-url-rewrite-module/.
My issue is that all of my initially examples redirect to www.example.com/ProductA. It is as if they are ignoring the Query Strings. Any idea how to fix this? My rule in my config file is:
<rule name="DbProviderTest" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{DB:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
the URL that is matched in the tag does not include the Query String and that is why you will not see it in your R:1, you should be able to change your condition to be something like:
<add input="{DB:{R:1}?{QUERY_STRING}}" pattern="(.+)" />

Resources