Setting up a redirect from MVC folder to another URL using IIS.

Today I was asked to setup a redirect from a url like http://www.mysite.com/Greensboro to the root of the website http://www.mysite.com/.

First of all this is a MVC4 site running on an IIS server. I am a developer, but in this case, the site is built by a 3rd party and while I have access to the IIS server, I don’t have access to the source code, so I can’t change route rules in the Global.asax. Being that it is MVC, I have issues I wouldn’t have in a non-MVC site (routing issues).

FYI: The business reason for this is that the 3rd party building this website won’t complete the code under the /Greensboro section before we release print materials with that url, so we need to send visitors to the root site rather than showing a 404 or other error message.

My first thought was to drop in a HTML page with a meta refresh redirect tag, but this being MVC, and because meta tags are 100% reliable, I skipped that.

My second thought was to add an Application under the site in IIS and then use the Http Redirect in IIS. This failed because of issues with the web.config file and dlls not being present in the application I created. I tried fixing the web.config and placing copies of the main bin in the application folder, but fixing each issue leads to another issue, (and on and on) and it is getting really messy, so I decided to look at other alternatives.

The final solution was quite simple so I won’t bore you with all the mis-steps.

  1. To make this work, create a virtual directory under your website (not an Application). I’ve named my virtual directory ‘Greensboro’ and pointed it to a new folder I created (you just create a new folder in your site and point it to that).
  2. In IIS, click on your new virtual Directory, then double click on the Http Redirect icon.
  3. Set the redirect route to where you want customer redirected (http://www.mysite.com/).
  4. Check the box for ‘Redirect all request to exact destination (instead of relative to destination)’
  5. Click ‘Apply’

So this works very nicely as a Virtual Directory, but can have you pulling your hair out if you were to use an ‘Application‘.

Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s