Solved

How to insert Win 2008 server IIS 7 redirect for a site or a virtual dir via command line?

Posted on 2014-10-29
3
209 Views
Last Modified: 2014-11-04
Hi there,

I have figured out how to add a url redirect for my entire site to a maint.html page on one site away from our servers to another site of mine that is somewhere else....by going into the IIS console and setting it up there via the console interface.

But when I have to do the maintenance, I need to do it on 8 or 10 sites.

It's slow going into the IIS console and wandering around in there to turn on these redirects.

I am guessing this can be done from a command line ... but I need some help with that.

Here is what ended up in my web.config on one of them... (domain is changed)

<httpRedirect enabled="true" destination="http://www.my-other-domain.com/maint.html" exactDestination="true" />

So, is there a way to issue this from a command line?

For example, if I have three sites: www.mysite-one.com  www.mysite-two.com  www.mysite-three.com

Say I wanted these three websites to redirect all requests to      http://www.my-other-domain.com/maint.html .. temporarily.

Can I do this from the command line?

And, I have a fourth site that I only want to redirect one virtual directory on it ..

So, I need to redirect all requests on www.mysite-four.com/virtualdir/ also to http://www.my-other-domain.com/maint.html

Please help!

Thx Scott :-)
0
Comment
Question by:srbarker8
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
choward16980 earned 500 total points
ID: 40411591
http://blogs.msdn.com/b/mcsnoiwb/archive/2012/04/20/configure-redirect-via-iis-using-command-line-script.aspx
@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:http://%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)

Open in new window


The way this works, create a batch file, paste this code.  When you run it, you have up to 4 arguments you can use.  Argument 1 is old URL (www.google.com/index.html), 2 is New URL (www.google.com/maint.html), 3 is port binding for old URL (in case 443) and 4 is port binding for new URL.
0
 
LVL 16

Expert Comment

by:choward16980
ID: 40411604
http://support.microsoft.com/kb/930909

HTTP redirection

To enable HTTP redirection, follow these steps:
Click StartStart button, type command prompt in the Start Search box, right-click Command Prompt in the Programs list, and then click Run as administrator.

User Account Control permission If you are prompted for an administrator password or for confirmation, type your password, or click Continue.

Type the following command, and then press ENTER:

cd \windows\system32\inetsrv

Type the following command, and then press ENTER:

appcmd set config /section:httpRedirect /enabled:true

To add an HTTP redirection rule, type the following command, and then press ENTER:

appcmd set config /section:httpRedirect/+[wildcard='WildcardHeader',destination='Destination']

Note In this command, WildcardHeader represents the Web page or pages that you want to redirect. Destination represents the destination Web page to which the WildcardHeader value redirects.

To disable HTTP redirection, type the following command, and then press ENTER:
appcmd set config /section:httpRedirect /enabled:false



appcmd set config /section:httpRedirect /enabled:true
appcmd set config /section:httpRedirect /enabled:false
0
 

Author Closing Comment

by:srbarker8
ID: 40422150
Thx a lot!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was supporting a handful of Windows 2008 (non-R2) 2 node clusters with shared quorum disks. Some had SQL 2008 installed and some were just a vendor application that we supported. For the purposes of this article it doesn’t really matter which so w…
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now