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
219 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 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

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

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

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…
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

705 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