Set IIS Redirect Settings using Powershell


Hi.

Today I had to change the redirect setting on IIS for a site and before it has been a kind of painful process.

I don’t like doing things manually because it is very error prone and mainly when I’m dealing with Production servers errors are not very welcome.

This powershell script can change these settings for you:

import-module webAdministration

$siteName = "Your Site"
$redirectPage = "http://yourredirectpage.com/destination.aspx"

# Set the redirect
Set-WebConfiguration system.webServer/httpRedirect "IIS:\sites\$siteName" -Value @{enabled="true";destination="$redirectPage";exactDestination="true";httpResponseStatus="Found"}

In order to disable redirect you can use this script:

<pre>import-module webAdministration

$siteName = "Your Site"
$redirectPage = "http://yourredirectpage.com/destination.aspx"

# Disable the redirect
Set-WebConfiguration system.webServer/httpRedirect "IIS:\sites\$siteName" -Value @{enabled="false"}

You can get the script file here.

See you.

Advertisements

2 Responses to Set IIS Redirect Settings using Powershell

  1. This doesn’t work on Virtual Directories, can you give an example of how to create an http redirect on a virtual directory ?

    • ascampanelli says:

      Hi.

      In order to do it, you need to change the script a little bit.
      This is a sample where your site is named Site1 and your virtual directory is named Dir1:

      import-module webAdministration

      $siteName = “Site1”
      $virtualDir = “Dir1”
      $redirectPage = “http://yourredirectpage.com/destination.aspx”

      # Set the redirect
      Set-WebConfiguration system.webServer/httpRedirect “IIS:\sites\$siteName\$virtualDir” -Value @{enabled=”true”;destination=”$redirectPage”;exactDestination=”true”;httpResponseStatus=”Found”}

      Check it out on your environment.

      Thanks,
      Amadeu.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: