URL Rewriting – Part 3: Integrating with SharePoint 2010

In this series of posts I’m going through the URL Rewriting process using the IIS URL Rewrite feature.

Check the previous posts out:
URL Rewriting – Part 1: The Basics
URL Rewriting – Part 2: Outbound Rule

On the previous posts we installed the feature and configured inbound and outbound rules using the IIS Rewriter. In this article we go further and show how to integrate the IIS URL Rewriter with a SharePoint site.

SharePoint 2010 Support for IIS URL Rewriter

If you search for IIS URL Rewriter on SharePoint you might see some articles about it not being supported on SharePoint 2010. It is kind of true but not completely. It will depend on the type of authentication your site is using. If you want to build a public internet site (anonymous authentication), you can use IIS URL Rewriter on your site with a few tweaks. If you’re building an intranet site (Windows or Claims authentication) you’re not going to be able to use IIS URL Rewriter.

When I first started trying to integrate IIS URL Rewriter and SharePoint 2010 I was building a public internet on my DEV environment and it was configured for both Windows and anonymous authentication. I noticed the anonymous part (mainly content and web part pages) worked fine but the administrative part (list management, user management and settings pages) didn’t. It would always ask me for authentication and it never had a consistent behavior. Every time I tried one of the administrative pages asked for authentication.

Installation

You should install the IIS Rewrite feature on all SharePoint WFE servers. The installation on SharePoint uses the very same process described on the first part of the series.

Tweaks for SharePoint 2010

Disable the LogRewrittenUrlEnabled property

On the WFE servers execute the following command:
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Rewrite /v LogRewrittenUrlEnabled /t REG_DWORD /d 0


Edit the web.config file

Add the following node under configuration/system.webServer in order to change compression settings:

<urlCompression doStaticCompression=”false” doDynamicCompression=”true” dynamicCompressionBeforeCache=”false” />

Restart IIS

Execute an IISRESET command on all WFE servers.

Testing Inbound and Outbound Rules

Edit the web.config file for your site on all WFE servers.

Add a new inbound and outbound rules to test on your site. Here is a simple example you can add to your web.config under the configuration/system.webServer node:

<rewrite>
  <rules>
    <rule name="Home Page Rewrite" stopProcessing="true">
      <match url="^(/$|$)" />
      <action type="Rewrite" url="/pages/default.aspx" />
    </rule>
  </rules>
  <outboundRules>
    <rule name="Home Page Outbound Rewrite" stopProcessing="true">
      <match filterByTags="A, Link" pattern="(/?)pages/default\.aspx$" />
      <conditions>
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </conditions>
      <action type="Rewrite" value="{R:1}" />
    </rule>
  </outboundRules>
</rewrite>

These rules will remove the home page redirect from the browser’s address bar and rebuild all the links to the home page to point to /.

References

http://codeblog.shawson.co.uk/iis7-urlrewrite-outbound-links-with-compression-enabled/

http://blogs.msdn.com/b/danielvl/archive/2010/01/07/registry-values-for-iis-url-rewrite.aspx

On the next articles of this series we will talk about custom IIS Rewriter providers and how to implement them.

See you,

Amadeu.

Advertisements

How to Schedule a Powershell Script to Run on Windows 2008

It is a little bit tricky to configure a Powershell  script to run on the task scheduler on Windows 2008.

The separation between the powershell.exe command and the arguments makes a little confusion.

The way I did it was:

  • Create a new task.
  • Define a schedule.
  • Add an action:
    • Program/script: Powershell
    • Add arguments: -command &{E:\scripts\ClearIISLogs.ps1}

Now you just need to run your script and make sure it works as expected.

 

See you,

Amadeu.

How to Check if .NET 4.5 Is Installed on a Server

In order to check if your server has .NET Framewrok 4.5 installed you can go to the Registry and look for the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client

Check the Version value. If it starts with “4.0” you are running on the 4.0 runtime, if it starts with “4.5” you are running on the 4.5 runtime.

See you,
Amadeu.

Retrieve a List of All Items Checked Out in a Site using Powershell

Hi there!

This is a scripts to list files which are currently checked out on a site collection. Some times users check files out and forget to check them back in. It was based on a script found at http://www.thorntontechnical.com/tech/powershell-tech/powershell-tip-find-all-checked-out-files.

$webAppURL = "http://yourwebapplication.com" 

function ReportCheckedOutItems() {
	$webapp = Get-SPWebApplication $webAppURL

	foreach ($site in $webapp.Sites) 
	{
		write-host "Site Collection: $($site.Url)"
		$allwebs = $site.allwebs
		foreach($web in $allwebs)
		{
			$count = 0
			write-host "--Site: $($web.Url)"
			foreach ($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
				Write-Host "-----List: $($list.RootFolder.ServerRelativeUrl)..."
				foreach ($item in $list.CheckedOutFiles) 
				{
					if (!$item.Url.EndsWith(".aspx")) 
					{ 
						continue 
					}
					write-host "File: $($item.Url) - checked out by $($item.CheckedOutBy)" -ForeGroundColor Red
					$count++
				}
				foreach ($item in $list.Items) 
				{
					if ($item.File.CheckOutStatus -ne "None") 
					{
						if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) 
						{ 
							continue 
						}
						write-host "File: $($item.Url) - checked out by $($item.CheckedOutBy)" -ForeGroundColor Red
						$count++
					}
				}
			}
			if ($count -gt 0)
			{
				write-host "Found $count checked out files for site $web" -ForeGroundColor Red
			}
		}
	}
}

ReportCheckedOutItems

 

You can download the script file here

I hope it is useful for you as it is for me.

 

See you,

Amadeu.

FAST ESP – Links to Offline Install Components

The default FAST ESP installation requires your computer to have an internet connection. In several environments it is not a reality and you then have 2 options: run the first part of the installer on a box with internet connection and stop it after it have downloaded all components or download them manually and copy then to the data sub-folder on the installer path.

 

Here are the links for the manual download option:

http://surfnet.dl.sourceforge.net/sourceforge/hibernate/hibernate-3.0.5.zip

http://www.jacorb.org/releases/2.2.1/JacORB_2_2_1-compact.zip

ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R5.zip

http://dbs.mathematik.uni-marburg.de/research/projects/xxl/zip/xxl_1_0.zip

http://espdownload.net/libiconv/iconv-1.9.2.17-win32-cl13.10.zip

http://espdownload.net/nux/nux-1.3.zip

http://espdownload.net/c3p0/c3p0-0.9.1.2.bin.zip

http://espdownload.net/cross_browser_x/x40_xc028b_z1.zip

http://espdownload.net/omniorb/omniorb-4.0.7.9-win32-cl13.10.zip

http://espdownload.net/omniorb-py/omniorb-py-2.6.22-win32-cl13.10.zip

http://surfnet.dl.sourceforge.net/sourceforge/wsdl4j/wsdl4j-bin-1.5.2.zip

 

See you,

Amadeu.

Turn Static and Dynamic Compression Off on IIS 6.0

We found an issue when downloading dynamic PDFs files on IIS 6.0. Since it uses a regular PDF as a template and binds the data dynamically on the client, IIS compression was modifying the file and it cause the bind to fail. In this case you need to disable IIS’ file compression (both static and dynamic) and the only way to do it on IIS 6.0 is through the IIS Metabase.
You can use metabase editor or edit it manually by the metabase’s XML file. The steps to edit the metabase file manually are the following:

 

Enable IIS Metabase direct edition

Go to IIS Manager.
Right click on the computer name an select Properties.
Check the Enable Direct Metabase Edit option and click OK.

 

Backup the metabase file

The metabase file is located at C:\windows\system32\inetsrv\metabase.xml

 

Edit the metabase file
Add the following attributes to the virtual directory (IIsWebVirtualDir node) or web site (IIsWebServer node):

DoStaticCompression=”FALSE”

DoDynamicCompression=”FALSE”

 

Save the metabase file

 

Reset IIS

Run the iisreset.exe command

 

Disable the IIS Metabase direct edition

Go to IIS Manager.
Right click on the computer name an select Properties.
Uncheck the Enable Direct Metabase Edit option and click OK.

 

See you,

Amadeu.

URL Rewriting – Part 2: Outbound Rule

In this series of posts I’m going through the URL Rewriting process using the IIS URL Rewrite feature.

Check the other posts:
URL Rewriting – Part 1: The Basics

 

On the previous post we installed the feature and configured an inbound rule, which basically is a friendly URL when requests come from the browser to IIS. In this post we are going to talk about other rule templates available on the IIS URL Rewrite. One of the rules we are going to see is the outbound rule which is when we process elements on the returning HTML to be set to the browser and transform URLs into friendly URLs.

 

Outbound Rules

Outbound rules control the way links should be rendered when IIS sends HTML back to the browsers after processing the requested page. It can be used to enforce SEO links on your site (friendly URLs) and handle business rules.

 

Outbound Blank Rule

Go to IIS Manager. Select your site.

In the URL Rewrite screen, click on “Add Rule(s)…” link on the Actions panel.

 

Select the Outbound Blank rule template.

When creating a rule you need to define:

  • Name: rule name.
  • Pre-Condition: the pre-condition defines if the outbound rule should be applied to the response IIS is sending to the browser. You can use different fields of the response to check on your pre-condition.

The pre-conditions can be reused between different rules.

  • Name: pre-condition name.
  • Using:
    • Regular expression: defines Regex as the parser for the expression defined on the pattern field.
    • Wildcards: defines the wildcards parser as the parser for the expression defined on the pattern field.
    • Logical grouping:
      • Match all: all conditions need to apply for the outbound rule to be applied.
      • Match any: if one condition applies the outbound rule is applied.
    • Conditions:

  • Condition input: HTTP header field to be checked. Examples: it can be the content type ({RESPONSE_CONTENT_TYPE}) or the response status ({RESPONSE_STATUS}).
  • Check if input string:
    • Matches the pattern.
    • Does not match the pattern.
    • Pattern: the pattern to be tested.
    • Ignore case: ignores cases when applying the pattern. It is a good setting to be left checked if you do lots of regular expression.
  • Match
    • Matching scope: defines if the outbound rule should be applied to the response header (server variables) or to the response body (response).
      • Response
      • Server variable
    • Match the content within: tags and attributes to apply the rewrite when processing the return HTML content.
    • Custom tags: specify your custom tags contained on the return HTML.
    • Content:
      • Matches the pattern.
      • Does Not Match the Pattern.
    • Using:
      • Regular Expressions: looks for a regular expression on the selected tags properties.
      • Wildcards: looks for a wildcard expression on the selected tags properties.
      • Exact Match: looks for a exact match on the selected tags properties.
    • Pattern: expression to be matched based on the type of expression selected on the Using field.
    • Ignore Case: ignores cases when applying the pattern. It is a good setting to be left checked if you do lots of regular expression.
  • Conditions
    • They are the same as pre-conditions but cannot be reused between outbound rules.
  • Action
  • Action Type:
    • Rewrite: applies the regular expression to the links found on the return HTML.
    • None: doesn’t execute any operation on the request, just closes it.
  • Stop processing of subsequent rules: stops the processing if the rule is applied.

 

Example of an Outbound Rule

This example rule is intended to modify links pointing to the product.aspx page rewrote to the /products/[product id]/[product name] format.

Here is the rule configuration:

This is the HTML code for my home page with the links to the products:

This is the returned HTML I can see on my browser:

This is how the page looks like. The rewrite feature has no effects on the page itself.

References about this topic can be found at  IIS.net.

 

Complexity of Regex

If you look at the outbound rule example you might find very complex to write Regex. Normally it is not very easy to write it if you’re not used with its syntax and it can lead you to make several mistakes and have problems debugging it.

 

If there is a tip I can give you it is: test it outside IIS and your web pages before trying to debug it in any other tool or way. A good, simple and free (some of the features are free) way to do it is using the Regex Hero web site. It has a nice UI and several features to allow you understand how the matches happen.

 

Conclusions

At this point we know how to create basic inbound and outbound rules in IIS using the IIS Rewrite module. This way we can make entire sites look friendlier for end users changing its URLs to have a more meaningful appearance. On the next articles we will look on how to create custom providers to have rules created outside of IIS and how to integrate the IIS Rewrite to SharePoint 2010.

 

See you,

Amadeu.