Partager via


Configure redirect via IIS using command line script

In our current project, we were moving an old SharePoint installation to a new farm, As part of this migration, we got a new, nicer DNS name. We wanted to support old users who had bookmarks etc., by doing a redirect. First of all, we configured both DNS’s to point to the same IP address, like this:

  • long-old-url.company.com –> 10.10.10.10
  • nice-url.company.com –> 10.10.10.10

This way, both new and old users could access the same backend transparently. But, we wanted the old users to be redirected to the new URL, so that going forward everyone would see “https://nice-url.company.com” in their browser address bar. Fortunately, version 7 of IIS added support for redirects. But it turns out that the redirect module is not enabled by default. You must go into the “Windows features” to enable it.

Once the redirect feature was enabled, it was easy enough to configure the redirect using the UI.

 

UPDATE:
I though it was this easy, but it turns out that query parameters are not passed along when using the straightforward redirect function, but you need to add $V$Q and check the "Exact destination", like this:

 

As we have multiple environments were this needs to be configured, we wanted to do it all via the command line. It took some research to figure out the steps to activate the windows feature and even more to do the IIS configuration via the command line. So, for your convenience, here is a complete batch script which takes as input four parameters and sets it all up in one go. The four parameters are:

  1. Old URL
  2. New URL
  3. Old port (defaults to 80)
  4. New port (defaults to 80)

And here is the complete script with inline comments to explain the required steps:

@echo off

REM Configuration
set oldUrl=%1
set newUrl=%2
if "%3" == "" (set oldPort=80) else (set oldPort=%3)
if "%4" == "" (set newPort=80) else (set newPort=%4)

set siteId=1001
set siteName=%oldUrl%
set virtualDir=c:\inetpub\wwwroot\wss\VirtualDirectories\%oldUrl%-%oldPort%

REM ************************************************
REM Enable HTTP Redirect feature
REM ************************************************
dism /online /Enable-Feature /FeatureName:IIS-HttpRedirect

REM ************************************************
REM Create virtual directory for site
REM ************************************************
mkdir "%virtualDir%"

REM ************************************************
REM Create site
REM ************************************************
%windir%\system32\inetsrv\AppCmd ADD SITE /name:%siteName% /id:%siteId% /bindings:http/*:%oldPort%:%oldUrl% /physicalPath:"%virtualDir%"

REM ************************************************
REM Add redirect setting to web.config
REM ************************************************
%windir%\system32\inetsrv\AppCmd SET config %siteName% /section:httpRedirect /enabled:true /destination:https://%newUrl%:%newPort%$V$Q /exactDestination:true

REM ************************************************
REM Grant read access to Everyone recursively
REM (Automatic when done via UI, but not via cmd)
REM ************************************************
icacls "%virtualDir%" /t /q /grant:r everyone:(GR)

ConfigureIISRedirect.cmd.txt