QFDev
QFDev

Reputation: 9008

.NET Generic Redirect from Folder

I have a folder containing multiple ASPX files as such

folder/file1.aspx
folder/file2.aspx
folder/file3.aspx
etc..

I need to move these files to another folder and change their extension to static html files as follows.

folder2/file1.htm
folder2/file2.htm
folder2/file3.htm
etc..

I'm trying to gracefully redirect the user and I'm unsure how to proceed? In .NET is it possible to intercept the request for the aspx file and redirect to the html equivalent in the new directory?

This also needs to be done programatically, I can't leave all the aspx files in the first folder with 301 redirects in the page_load. How can I grab the call to folder/any_resource, get the name of the resource, replace the .aspx for .htm and redirect to the new directory with the amended resource name?

Is this at all possible?

I am using the .NET framework version 4 on IIS 7.

Upvotes: 2

Views: 194

Answers (1)

Daniel Wardin
Daniel Wardin

Reputation: 1858

Have a look into URL Rewrite functionality. IIS7 supports it. It can be done through web.config file or you can use the IIS GUI to create the rules as well.

Pretty much what it does is, if you try to access folder/page.aspx server will automatically fetch folder2/page.aspx.

You can either supply an exact match for each page (tedious) or you can use a regular expression and get it done with one rule (provided all pages have same names in the next folder.)

What you want to do is catch anything going to folder/.aspx and make the server return folder2/.aspx.

So your regular expression for catching requests to folder//aspx might look like this:

folder/(\w*).aspx

Brackets around \w* will make it available for back referencing ( {R:1} )

folder2/{R:1}.htm

The above is your Redirect URL. What it does is it takes the file name and looks for .htm version of it in folder2.

If you want to redirect everything simply take away .aspx and .htm. So:

folder/(\w*)

and

folder2/{R:1}

Here is a quick tutorial that shows you where to do it in IIS. Make sure to select Redirect instead of Rewrite under Action type when creating the Rule.

URL Rewrite

Upvotes: 1

Related Questions