URL Rewriting and Anonymous Access with SharePoint – Part Three

23 Mar

IIS 7 Re-Write Module

At the moment our URL looks like http://geeksinsuits.com/CA/Pages/default.aspx and we want it to look like http://geeksinsuits.com/CA/default. Before we can do anything further we need to download and install the IIS7 Re-Write module from here.

The IIS Rewrite module uses regular expressions to match patterns and split the URL into bits that we can rewrite what we need back in. Regular expressions (regex) can be hard to pick up, but you don’t need to be an expert, I’m certainly not. Trial and error work OK as well. Here’s a reference for the basics here.

  1. Make sure the page we will be testing with is published and can be accessed anonymously so if there are problems with the re-writing logic we know it’s not an anonymous problem. As I said earlier the easiest way of testing this is by having two browsers, IE for content editing and perhaps Firefox for viewing the anonymous site.
    1. Go to http://admin.geeksinsuits.com/CA/
    2. Click on Site Actions, View all Site Content and then Pages. This will show us what is published to the public
    3. If the page is still in draft it will need to be published and approved otherwise we will get a 404 error. Changes will only be picked up anonymously if they are again published and approved
    4. Now that we have a published page browse to http://geeksinsuits.com/CA/pages/default.aspx. This will be our page for testing the re-writing on

Incoming Rules

There are two incoming rules we want to have, one will be accepting the URL without the pages library and file extension, the other rewriting the default / welcome page for the site collections. Another optional rule is to rewrite SharePoint’s URL’s into our shortened URL’s.

NOTE:  If after following the below steps you continuously get asked for credentials, try re-opening the browser. If this does not work, before spending lots of time trouble shooting, try re-installing the re-write module as this can sometimes become corrupted and not work somehow…

  1. Write our first rule for the pages library and file extension
    1. Open IIS and navigate to the public facing site
    2. Double click on URL Rewrite
    3. There will be two empty lists, incoming rules and outgoing rules. For now we will only focus on incoming rules. Click on Add Rules and select Blank Rule
    4. Set the Name to “Rewrite pages and file extension” or something similar
    5. The first rule we will add will allow us to navigate to http://geeksinsuits.com/CA/pages/default.aspx as http://geeksinsuits.com/CA /default.
    6. In the Pattern field add ^(.*?[^/]+)/(.*?)$
      To test if the pattern is going to work click on test pattern and add the URL you want to check. Below I have entered the URL of our test page. You can see that it has been split into R:1 and R:2, we can now add the pages library in between here as well as the ending file extension. Again if your site is just one site collection follow Waldeks guide as he covers the rules for this scenario here.
    7. Tick Ignore Case
    8. Make sure the action is on Rewrite
    9. In the  Rewrite URL field add {R:1}/pages/{R:2}.aspx. This will piece together the actual URL of the page we are requesting
    10. Tick Stop processing of subsequent rules, if this matches we don’t need to process any more rules
    11. If you applied the changes and had a look at the page now it would look very messy. What we need to do is add a condition to ignore files so they aren’t rewritten.
    12. This rule was taken from Waldeks blog which steps through the same process. Under the conditions click on Add. For the condition input type {REQUEST_URI}.  Set the Check if input string field as Does Not Match the Pattern. Set the pattern to ^.*?[^/]+\.[^/]+$
    13. Apply the changes
    14. Now navigate to http://geeksinsuits.com/CA/default
  2. Because the pages library needs to have a default page and the site collection will point to this default page we need a rule to ensure this is being rewritten as well. This means http://geeksinsuits.com/ca/will go to the default page.
    1. Go to the IIS Rewrite module and add another blank rule
    2. Name the rule “Rewrite Default Welcome Page”
    3. Set the Pattern as ^(.*/)?$
    4. Ignore the case
    5. Set the rewrite URL as {R:1}Pages/default.aspx
    6. Stop processing of subsequent rules
    7. Apply the changes and try access http://geeksinsuits.com/CA/ you will get a 404 error. So we will move this rule up to the top so it gets processed first.
    8. Go to the list of incoming rules and select the “Rewrite Default Welcome Page” rule. Click on Move up
    9. If you get a prompt warning about the inheritance of the rewrite module click yes
    10. Go to http://geeksinsuits.com/CA/. This will load the default page
  3. Optional: Redirect SharePoint URL’s to readable URLs
    1. In case someone enters the full URL or there are links still hard coded with the pages library and .aspx extension we can rewrite this when the page is loaded.
    2. Create a new blank rule
    3. Set the name as “Redirect incoming sharepoint Url into readable Url” or something similar
    4. Set the pattern to ^(.*)/?/Pages/(.*)/?\.aspx(.*)/?$
    5. Ignore the case
    6. Change the action type to redirect
    7. Set the redirect URL to {R:1}/{R:2}{R:3}
    8. Make sure the redirect type if permanent
    9. Now go to http://geeksinsuits.com/CA/Pages/Default.aspx and it will change to http://geeksinsuits.com/CA/Default

Why is there still some ugly stuff? Next post… (Creating Outbound Rules for hyperlink URL’s)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: