Feeds:
Posts
Comments

Archive for May, 2017

To have http redirects in a sharepoint we can use something like this in the web.config file

<location path=”oldpage1.htm”>
<system.webServer>
<httpRedirect enabled=”true” destination=”newpage1.html” exactDestination=”true” httpResponseStatus=”Permanent” />
</system.webServer>
</location>

One simple way of doing it using PowerShell is to use the SPWebConfigModication object.

The following script can achieve that

 

$Owner = “redirectMod”
$webApp = Get-SPWebApplication $Url

 

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration”;
$modification.Name = “redirect-1” ;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “<location path='”+$from+”‘><system.webServer><httpRedirect enabled=’true’ destination='”+$to+”‘ exactDestination=’true’ httpResponseStatus=’Permanent’ /></system.webServer></location>”;
$webApp.WebConfigModifications.Add($modification);

$webApp.Update()
$webApp.Parent.ApplyWebConfigModifications()
do{
Start-Sleep -Seconds 2;
Write-Host “.” -NoNewline
$configJobRunning = IsWebConfigModificationJobPendingOrRunning $webApp;
}while ($configJobRunning);

 

Read Full Post »

If we need to add some URL rewrite to a SharePoint site, we can do that using some web.config settings like :

<rewrite>
<rewriteMaps>
<rewriteMap name=”Redirects”>
<add key=”/Page1.html” value=”/Page1″ />
<add key=”/Page2.html” value=”/Page2″ />
</rewriteMap>
</rewriteMaps>
</rewrite>

One way to do that Is to use SharePoint Powershell commands.

The following script achieves that:
[Microsoft.SharePoint.Administration.SPWebConfigModification] $modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer”;
$modification.Name = “rewrite”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite”;
$modification.Name = “rewriteMaps”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite/rewriteMaps”;
$modification.Name = “rewriteMap”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite/rewriteMaps/rewriteMap”;
$modification.Name = “redirect-1”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite/rewriteMaps/rewriteMap”;
$modification.Name = “redirect-2”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$webApp.Update()
$webApp.Parent.ApplyWebConfigModifications()

do{
Start-Sleep -Seconds 2;
Write-Host “.” -NoNewline
$configJobRunning = IsWebConfigModificationJobPendingOrRunning $webApp;
}while ($configJobRunning);

References:

https://blog.kenaro.com/2010/09/02/add-web-config-modification-with-powershell-spwebconfigmodification/ 

 https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module 

Read Full Post »