Step 0: Introduction
I need to redirect this URL (with underscores):
https://www.example.com/video_83546_1/pearl_jam_hold_on.htm 
to this URL (with dashes):
https://www.example.com/video83546/pearl-jam-hold-on.htm
 
Step 1: Create C# Project
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Iis.Rewrite;
public class ReplaceProvider : IRewriteProvider, IProviderDescriptor
{
    char oldChar, newChar;
    #region IRewriteProvider Members
    public void Initialize(IDictionary settings, IRewriteContext rewriteContext)
    {
        string oldCharString, newCharString;
        if (!settings.TryGetValue("OldChar", out oldCharString) || string.IsNullOrEmpty(oldCharString))
            throw new ArgumentException("OldChar provider setting is required and cannot be empty");
        if (!settings.TryGetValue("NewChar", out newCharString) || string.IsNullOrEmpty(newCharString))
            throw new ArgumentException("NewChar provider setting is required and cannot be empty");
        if (!string.IsNullOrEmpty(oldCharString))
            oldChar = oldCharString.Trim()[0];
        else
            throw new ArgumentException("OldChar parameter cannot be empty");
        if (!string.IsNullOrEmpty(newCharString))
            newChar = newCharString.Trim()[0];
        else
            throw new ArgumentException("NewChar parameter cannot be empty");
    }
    public string Rewrite(string value)
    {
        return value.Replace(oldChar, newChar);
    }
    #endregion
    #region IProviderDescriptor Members
    public IEnumerable GetSettings()
    {
        yield return new SettingDescriptor("OldChar", "Old Character");
        yield return new SettingDescriptor("NewChar", "New Character");
    }
    #endregion
}
 
Step 4: Install the dll onto your server
INSTALL DLL (ReplaceProvider.dll):
Set-location "C:\WebsiteGac"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\WebsiteGac\ReplaceProvider.dll")
iisreset
 
Step 5: Add the Provider to IIS
Step 6: Add <settings> to the <provider> object in the Web.Config file
Step 7: Create the Re-Write Rules
Step 7a: Re-Write rule that handles ALL URLS on the entire website
This will replace dashes with underscores in ALL URLS:
 
Step 9: UN-install the dll from your server
UN-INSTALL DLL (in case you want to rollback):
Set-location "C:\WebsiteGac"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacRemove("C:\WebsiteGac\ReplaceProvider.dll")
iisreset
 
File Downloads
Back to Top ↑