IIS 8.5 redirect rules and how to make them

iis-8.5

Got few domains running on one box, everything with SSL, I am trying to learn this stuff

I have domain1.com domain2.com domain3.com everything works.

I want to achieve this:

If users will fill IP of the box in the browser, I want the users to get redirected to google for an example. Otherwise it shows warning about non-ssl. Basically, I dont want users to be able access this box with direct IP, I want them redirected to google, all of them.

I know how to do it on apache, no idea how to do it on IIS 8.5.

thank you very much

Best Answer

There are multiple ways to do this, my solution uses only built-in modules.

The basic idea is that for all your proper content sites, you only use host name bindings. This way they are not accessible using an IP address.

Then you use an extra site, which you don't bind to a hostname but to all IP addresses on the server. That site handles all requests that are not picked up by any of the other web sites. On that site you redirect everything to Google.com

Here's how to do this, I'm using PowerShell commands so make sure you have that IIS-PowerShell module installed:

Enable-WindowsOptionalFeature -online -FeatureName IIS-ManagementScriptingTools

I'm using the pre-installed Default Web Site as the catch-all site, confirm that the site is bound to all IP addresses:

ls IIS:\Sites\

you should get something like:

Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:

http *:80: means port 80 on all IP addresses without a hostname.

You need the http-redirect module:

Enable-WindowsOptionalFeature -online -FeatureName IIS-HttpRedirect

Now set up the redirection for all requests to the site:

 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/httpRedirect" -name "enabled" -value "True"
 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/httpRedirect" -name "destination" -value "https://www.google.com"
 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/httpRedirect" -name "exactDestination" -value "True"

Now all requests to any IP addresses on the server should redirect to google.com, you can now add additional sites with bindings to hostnames.

When dealing with https, it gets a little bit more complicated, but using https with an IP address will result in a certificate error anyways.

Related Topic