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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
URL Redirect using IIS v8 4 53
SHA2 certs for IIS AND Java? 2 80
Disable SSLv3.0/TLSv1.0 - Windows 2012R2 3 31
How to search SamAccount in AD but filtered by mail? 8 46
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…
OfficeMate Freezes on login or does not load after login credentials are input.
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…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

806 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