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
205 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thx a lot!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now