Route static files to new path in asp.net mvc - asp.net-mvc

I have 1000 static html page files and html data in Database.
Now i want to move to amazon s3 server, i will move all of my static files to s3.
How i can route the path to the new location, i can't update all of the pages and correct images path.
like:
<img src='/myfiles/images/blog/20140606/flow.jpg'/>
to
Request: /myfiles/images/blog/20140606/flow.jpg
Redirect to: htps://xxx.s3.amazonaws.com/myfiles/images/blog/20140606/flow.jpg
i need a route to redirect the request to new location instead of update all files.

May be using a url rewrite in your web.config like this:
<rewrite>
<rules>
<rule name="Rewrite to s3" stopProcessing="true">
<match url="^/myfiles/images/blog/(.*)" />
<action type="Rewrite" url="s3/mybucketname/{R:1}" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>

Related

IIS rewrite - redirect path to a different URL, including partial path (C# web config)

I have a multisite setup in Sitecore, where my main site is mysite.com and I have a secondary site of secondsite.org. These both work, but the user can also navigate to secondsite.org by going to the Sitecore path for the second site home node, i.e. mysite.com/second-site-home. I don't want this to happen, so I added a redirect in the webconfig so that mysite.com/second-site-home will redirect to secondsite.org:
<rule name="homepage path Redirect" stopProcessing="true">
<match url="^second-site-home/?(.*)$" />
<action type="Redirect" url="http://secondsite.org" redirectType="Found"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mysite.com$" />
</conditions>
</rule>
This works fine for the homepage, but I have links on mysite.com that go to pages on secondsite.org, and these links are rendering as mysite.com/second-site-home/about, and when I navigate to this link I get redirected to the secondsite homepage, rather than the actual page I want to go to.
Is is possible to modify the redirect so that in the redirect url, it includes the rest of the path after /second-site-home/ ?
You just need to add a back reference variable to your rule:
<rule name="homepage path Redirect" stopProcessing="true">
<match url="^second-site-home/?(.*)$" />
<action type="Redirect" url="http://secondsite.org/{R:1}" redirectType="Found"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mysite.com$" />
</conditions>
</rule>

IIS URL Rewrite multiple parameters

I am really confused about the URL rewrite interface. I dont understand what i need to do.
I have an url as:
www.example.com/diretory/subdirectory/index.html?param1=1&param2=2&param3=2&param4=7
I want to hide this url in a <a>-href tag, which displays "Example Tag".
When the url is requested it should rewrite it to
www.example.com/program/location/year/vacancie
I allready tried this:
<system.webServer>
<rewrite>
<rules>
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
<action type="Rewrite" url="www.example.com/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
In the URL Rewrite Interface the Test Pattern said it is working and gets:
?param1=1&param2=2&param3=2&param4=7
param1=1
param2=2
param3=2
param4=7
I checked the log url rewrite as well, but in my logs it is not shown.
2017-03-20 16:29:24 192.168.253.146 GET /diretory/subdirectory/index.html param1=1&param2=2&param3=2&param4=7 88 - 192.168.253.146 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/56.0.2924.87+Safari/537.36 - 304 0 0 4
ps: the urls are not working and only for illustrative purpose.
The match URL only matches the URL and does not take the querystring into account. You will need to add a condition for this. Also do you want to rewrite this (so the server internally executes the new URL) or redirect (so the server will request the browser to go to the new URL and URL changes in address bar). In case you want to rewrite you should not add the domain again, in case you want to redirect add http:// as well. Assuming a rewrite is what you want use below rule:
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
</conditions>
</rule>

Rewrite images directory IIS

We are migrating a legacy application to ASP.NET MVC, but in the old application the images folder was in the root of the app, so to reference an images the path would be /images/imagename.png, but we want to move the images in to /Content/images in the new application but we dont want to change the paths in all the css and the html.
Is there a way to do a rewrite to the images folder so that every URL that goes to /images goes to /Content/images instead?
Use URL Rewrite. You need to install it via WebPI, then add the following to your web.config in system.webServer:
<rewrite>
<rule name="Old Images" stopProcessing="true">
<match url="^images/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="content/images/{R:1}" appendQueryString="true" />
</rule>
</rewrite>

IIS rewrite virtual folder

I need to create a URL rewrite rule in IIS for the following:
From:
http://hostname/virtual_path_folder/myisapi.dll?a=1&b=1
To:
http://hostname/myisapi.dll?a=1&b=1
Basically, I'd just like to hide the virtual_path folder if possible.
You could go with the 2 following rules:
<rules>
<rule name="Redirect if virtual_path_folder" stopProcessing="true">
<match url="^virtual_path_folder/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="virtual_path_folder/{R:0}" />
</rule>
</rules>
The first one, Redirect if virtual_path_folder, will redirect every request starting with virtual_path_folder/. It will prevent anyone from accessing your content using the sub folder.
The second one rewrites any request (^.*$) to the sub folder: virtual_path_folder/{R:0}

ASP.NET MVC 4 public folder like in Ruby on Rails

Is there a way to give an ASP.NET MVC 4 project a "public" folder, which would act like the application root, like in Ruby on Rails?
ASP.NET MVC usually comes with a "Content" folder where you can put statically served files like style sheets, scripts, and images. But the URLs for these files have to include the Content folder, for example <img src="/Content/logo.png">.Is it possible to make the Content folder the root of the application so you can use this instead: <img src="/logo.png">?
I'm not sure why I didn't think of this before, but I was able to accomplish this with IIS URL Rewrite. The following rules assume a folder named "Public" where your static files reside. Of course, you can name the folder whatever you want.
<!-- Any direct references to files in the Public folder should be
301 redirected to maintain canonical URLs. This is optional. -->
<rule name="Public folder canonical path" stopProcessing="true">
<match url="^public/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" appendQueryString="true" />
</rule>
<!-- Any URL that points to an existing file after prepending "Public" to it
will serve that file. For example, if a file exists at /Public/style.css
then the URL /style.css will serve that file. Likewise, if a file exists
at /Public/images/logo.png, then the URL /images/logo.png will serve
that file. Files in the Public folder will take precedence over files in
the application root, so if a file /Public/script.js exists and a file
/script.js exists, only the /Public/script.js file will be served. This
also takes precedence over MVC routes. -->
<rule name="Public folder" stopProcessing="true">
<match url=".+" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/Public/" negate="true" />
<add input="{DOCUMENT_ROOT}/Public{URL}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="Public/{R:0}" appendQueryString="true" />
</rule>

Resources