98

I am using Windows Server 2008 with IIS7. I need to redirect the users who come to www.mysite.com to wwww.mysite.com/menu_1/MainScreen.aspx. Here is the file structure I have for the projects:

-Sites
 -Default Web Site
  -Menu_1
  -MenuService
  -VscWebService

I will really appreciate any help on this.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
user881148
  • 1,209
  • 2
  • 9
  • 15
  • There's a fine line between programming and server administration (a line that's increasingly blurred thanks to the popularity of "Dev Ops"). Even so, Stack Overflow and Server Fault maintain a clear distinction between the two sites, and this content belongs on Server Fault as it's really more about configuring a tool (IIS) than programming. – machineghost Jun 29 '19 at 16:21

4 Answers4

161

Here it is. Add this code to your web.config file:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Root Hit Redirect" stopProcessing="true">
                <match url="^$" />
                <action type="Redirect" url="/menu_1/MainScreen.aspx" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

It will do 301 Permanent Redirect (URL will be changed in browser). If you want to have such "redirect" to be invisible (rewrite, internal redirect), then use this rule (the only difference is that "Redirect" has been replaced by "Rewrite"):

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Root Hit Redirect" stopProcessing="true">
                <match url="^$" />
                <action type="Rewrite" url="/menu_1/MainScreen.aspx" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
BurnsBA
  • 4,347
  • 27
  • 39
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 2
    Thank you for the response. It is still not working for me. First I added this to the wwwroot web.config and didn't work there. So I opened up the URL Rewrite at the Server Level ( Server > Sites > Default Website) but it isn't still working. Am I doing something wrong here? Thanks a lot again for your help. – user881148 Aug 15 '11 at 14:23
  • 2
    @user881148 The rule definitely working -- I tested before posting. 1) Maybe it is placed in wrong order/place? Order of rules matters: if you already have some rules then add this rule before other rules. 2) _"Server > Sites > Default Website"_ -- that's not Server Level -- it's default normal website created by IIS installer -- nothing more than that. 3) Try creating this rule manually via GUI (IIS Manager) -- it's simple -- only few fields to fill. 4) Maybe your ASP.NET routing handles all rewriting and ignores URL Rewrite module. Check this moment somehow (sorry, I'm not good at ASP.NET). – LazyOne Aug 15 '11 at 14:45
  • Thank you again for your reply. I checked everything you have mentioned above, and there are no other rules added. I tried adding this through Default Website > URL Rewrite and it didn't work either. I also don't have any ASP.NET rerouting set up. – user881148 Aug 15 '11 at 17:25
  • I am getting 500 - Internal server error when I access www.mysite.com. But if I access www.mysite.com/menu_1/MainScreen.aspx, it works fine. – user881148 Aug 15 '11 at 19:24
  • @user881148 Unfortunately I cannot advise on why it works for me and does not work for you. Maybe it is the way how you setup your application/site? Maybe URL Rewrite module is disabled/ignored for some reason. I never had any problems with URL Rewriting therefore it's hard to suggest anything viable. I can only suggest creating separate website and try it there -- because you set it on Default Website it may have some other modules interfering somehow, not sure. – LazyOne Aug 15 '11 at 19:29
  • @user881148 To diagnose 500 error enable displaying detailed error message -- it is possible that you have messed up the web.config file (like, deleted extra character .. or inserted some config into wrong place etc) -- start with removing web.config, and if works see inside for what may be wrong (if you wish -- update question with web.config content and I may have a look at it). – LazyOne Aug 15 '11 at 19:33
  • It is working for me now. I had to change the Action type to Redirect instead of Rewrite to redirect it to MainScreen.aspx file. I was trying many different things, and something I had done might have messed up the configuration. As you suggested, I started all over again and it worked with Action type=redirect. Thank you so much for your help. – user881148 Aug 22 '11 at 14:25
  • Oh, and I also had to disable ASP.net Impersonation for it to work - not sure why. – user881148 Aug 22 '11 at 14:26
  • I know it's very old, but I have a question: First, it works for me, so thank you! But can you explain this: `` This is "start of string" and right after that "end of string". So doesn't that match an empty string?! – Munchkin Mar 17 '16 at 07:49
  • @Munchkin Correct. When you hitting website root (e.g. `http://www.domain.com/`) the path would be empty hence the `^$` (the leading slash is removed from the path automatically; same in Apache for example). – LazyOne Mar 17 '16 at 09:00
  • @LazyOne, If I use second method i.e. Rewrite method, my page images not loading. It refers to http://xxx.xxx.x.x/Images/logo.png instead of referring http://xxx.xxx.x.x/menu_1/Images/logo.png. How to refer the sub directory path if I am implementing root hit direction technique? – RGS Jan 19 '17 at 17:36
  • @RGS did you ever figure this out? I know it has been a long time since you asked but maybe you recall a solution for your question about the images? – Jared Jun 28 '18 at 21:16
  • I had to install URL Rewrite from https://www.iis.net/downloads/microsoft/url-rewrite before this would work. – mythofechelon Aug 14 '18 at 09:09
  • 1
    @mythofechelon Yes, URL Rewrite module is still not bundled, even with IIS 10 (Windows 10 / Windows Server 2016) and has to be installed separately. – LazyOne Aug 14 '18 at 09:16
  • 2
    I would suggest using To ensure it will work if / is indicated by user in end of URL. – Marijus Ravickas Nov 13 '19 at 09:33
53

I think, this could be done without IIS URL Rewrite module. <httpRedirect> supports wildcards, so you can configure it this way:

  <system.webServer>
    <httpRedirect enabled="true">
      <add wildcard="/" destination="/menu_1/MainScreen.aspx" />
    </httpRedirect>
  </system.webServer>

Note that you need to have the "HTTP Redirection" feature enabled on IIS - see HTTP Redirects

BurnsBA
  • 4,347
  • 27
  • 39
Shad
  • 4,423
  • 3
  • 36
  • 37
16

I could not get this working with the accepted answer, mainly because I did not know where to enter that code. I looked everywhere for some explanation of the URL Rewrite tool that made sense, but could not find any. I ended up using the HTTP Redirect tool in IIS.

  1. Choose your site
  2. Click HTTP Redirect in the IIS section (Make sure the Role Service is installed)
  3. Check "Redirect requests to this destination"
  4. Enter where you want to redirect. In your case "wwww.mysite.com/menu_1/MainScreen.aspx"
  5. In Redirect Behavior, I found I had to check "Only redirect requests to content in this directory (not subdirectories), or it would go into a loop. See what works for you.

Hope this helps.

Community
  • 1
  • 1
Jarrod
  • 1,535
  • 3
  • 16
  • 19
  • 1
    There is a problem with this solution. If set to redirect/rewrite `http://server/` to `http://server/Folder/`, accessing `http://server/Folder` (note no trailing slash) will be redirected to `http://server/Folder/Folder`, which will likely return a 404. – arid1 Dec 30 '13 at 19:29
  • It has been a while since I worked on this, but I seem to remember that was happening to me and the reason for my step #5. Do you have that checked? – Jarrod Jan 14 '14 at 20:17
  • 1
    I honestly can't remember, but I believe so. Note that in my case above IIS sees /Folder as a file instead of a directory, so it redirects to the subdirectory and wouldn't covered by the "(not subdirectories)" exclusion. – arid1 Jun 06 '14 at 20:33
4

You need to download this from Microsoft: http://www.microsoft.com/en-us/download/details.aspx?id=7435.

The tool is called "Microsoft URL Rewrite Module 2.0 for IIS 7" and is described as follows by Microsoft: "URL Rewrite Module 2.0 provides a rule-based rewriting mechanism for changing requested URL’s before they get processed by web server and for modifying response content before it gets served to HTTP clients"

Lizz
  • 1,442
  • 5
  • 25
  • 51
mikem
  • 49
  • 1