?
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
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
choward16980 earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
A phishing scam that claims a recipient’s credit card details have been “suspended” is the latest trend in spoof emails.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

770 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