Monday, September 23, 2013

Ways to redirect http requests in SharePoint

The are multiple szenarios when you need a redirect in SharePoint:

  • You've migrated SharePoint into a new URL (e.g http://sp2010 to http://sp2013)
  • You moved a site collection
  • You moved a sub site into a new site collection

If you want to redirect in SharePoint without custom solutions you have the following options:


Redirect with XML Viewer or Content Editor WebPart


You can redirect with JavaScript by inserting a WebPart into the site and adding simple JavaScript. This is easy to configure but redirects only the site in which the WebPart sits (Probably the Mainsite). If users have saved links the their document libraries this method fails, unless you put the same WebPart into the document library.

So go to the site you want to redirect and the XML Viewer WebPart. Open the WebPart settings and add the following line of code:

<script type="text/javascript">
location.href = "<<the url you want to redirect>>";
</script>



One drawback of this way to redirect is that you don't have an easy on/off button to activate this redirect. Once the WebPart is saved you can't edit the site anymore, because you're always redirected.

If you want to edit the site you'v to remove the WebPart by typing the URL and adding the querystring ?contents=1 to the site url like http://sharepoint/myproject?contents=1

You can also use the html meta tag "refresh" to redirect your page. Here is an example with a popup indicating that the page has moved and the meta tag.


IIS HTTP Redirect


This is a feature in IIS which have to be enabled. It is used to redirect all requests to another destination. This fits perfect when you have upgraded SharePoint.

Check within the Server Manager in the Web Server Role if this feature is already installed. Otherwise you can install it also from the command line:

dism /online /Enable-Feature /FeatureName:IIS-HttpRedirect

After you've enabled this feature you'll see it in the IIS Manager.


The HTTP Redirect Feature


Click on your SharePoint webapplication and then on "Http Redirect" to open the feature. 

In this example we migrated to from SharePoint 2010 to SharePoint 2013. The new url ist http://sp2013. So when someone enters the url http://sp2010/sites/myproject he should be redirected to  http://sp2013/sites/myproject.

Configure Redirect

If you want to do more granular redirect like including query parameter you can use parameter like $V and $Q. 

When you edit and apply redirect clear your browser cache for testing.

References:


Redirect with URL Rewrite 2.0 Module


The URL Rewrite 2.0 Module is not a feature in IIS 7.5. It has to be downloaded and installed from here. Once installed you can see the module within you features in IIS.

URL Rewrite module


This is a very might tool as you can use wildcard and regular expression. This means that you can also redirect untill item level in SharePoint.

In the following example I moved a site "projectx" under http://sharepoint into an own sitecollection http://sharepoint/sites/projectx.

Open the URL Rewrite Module in IIS within your webapplication. Click on "Add Rule" on the right pane. Add a name for your rule like "Redirect Projectx into Sitecollection"

The pattern must be server relative. Check Using "Wildcards" as we use asterisks "*" within the pattern.

In the action pane select "redirect" and the type the url you want to redirect. I added also "{R:1}" at the end of the redirect. This is a parameter which delivers all the right side from the pattern. So you can redirect all request under http://sharepoint/projects/xxxxx to http://sharepoint/sites/projectx/xxxxx.

You can see the parameters when you test the pattern by clicking "Test Pattern".


The rewrite module provide lots of more options for a granular redirect. You can check within the link I've provided in the reference.

Please not that when you apply a rule the web.config of the webapplication is changed and you get an IIS refresh. Another thing to pay attention is a when you change a rule, the rule is applied when you disable and enable it again.

References:

No comments: