1

I am decommissioning a company website (HTML), the company has re-branded and has a new site on a different domain/platform. I have to redirect(301) almost a 1000 individual pages and I really don't want to have to use the GUI to add every page so I am trying to find out if there is a command line facility that I can use to script the changes? At the moment the source and destination URLs are stored in a CSV.

Any thoughts or pearls of wisdom gratefully received.

Rob

RobC_CTL
  • 33
  • 8
  • The rules are just XML nodes in the web.config. You can use PowerShell to read the CSV file and create the XML nodes you needed. If the pages follow a certain pattern, you can also use regular expressions to have fewer rules. – Peter Hahndorf May 26 '22 at 15:47
  • Nice idea, however my PS skills aren't as sharp as they could be, do you know of any examples that I could look at? – RobC_CTL May 26 '22 at 17:22
  • No specifically, but lots of examples out there about handling CSV files and XML. Or find someone who can do it for you. – Peter Hahndorf May 26 '22 at 18:12
  • I suggest you open a case via: https://support.microsoft.com. – samwu May 27 '22 at 08:03

1 Answers1

1

SOLUTION

Thanks to Peter putting me on the right track, I was able to get this working and to help in the future these are the steps.

  • Install the URL Rewrite module into IIS, I created a dummy rule to I could see where the XML elements needed to go.

  • Create a CSV with the following headers: Name, Source, Target

Name has to be unique, the Source is the HTML page name and the Target is where you want the page to redirect to.

Create a PS1 file by copying the following:

$docTemplate = @'
<contact $($contacts -join "`n") />
'@
$entryTemplate = @'
                <rule name="$($redirect.Name)" stopProcessing="true">
                    <match url="$($redirect.Source)" />
                    <action type="Redirect" url="$($redirect.Target)" redirectType="Permanent" />
                </rule>
'@

Import-Csv C:\temp\Sites.csv -Delimiter ',' | Group-Object Id -ov grp | ForEach-Object {
  $contacts = foreach ($Redirect in $_.Group) {
    $ExecutionContext.InvokeCommand.ExpandString($entryTemplate)  
  }
 $ExecutionContext.InvokeCommand.ExpandString($docTemplate) } | 
  Set-Content -LiteralPath file.xml

I did steal this from another Stackoverflow post (link) and modify it to fit my requirements, you can ignore the "contact" element, it needs to remain but isn't used.

When you run the PS1 file you will end up with a file called "file.XML" that you can then copy all the rules and paste them into the web.config file.

Warning, if you are doing lots of re-writes you will hit an issue with the web.config file being over 250k in size - results in a 500 error. To fix this you need to edit the registry and add a couple of keys to allow for a bigger web.config file:

Key1: HKLM\SOFTWARE\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB This is a DWORD, set the value to decimal and change the value to a bit larger then your web.config.

Key2: HKLM\SOFTWARE\Wow6432Node\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB

Same as above, DWORD and set to a bit bigger than web.config file.

Finally do a IISRESET to pickup the changes.

Cheers Rob

RobC_CTL
  • 33
  • 8