SharePoint 2013: URL Rewrite
Description
This article is a spin-off from a Microsoft TechNet article I had recently published called Search Service Application Issue with AAM. In this article I am going to focus on how you can re-direct incomplete URLs to the correct URL of your choice. The steps provided in this article works for both anonymous access sites and those that require authentication.
Setting the scene
You have a site built on an on-premise SharePoint farm. The primary URL of this site is http://sp2013test.contoso.com/. Thanks to Alternate Access Mapping and IIS bindings you can now access this site as www.domain.com or http://www.domain.com/ where all the components such as the custom designed master page, search, taxonomy term, ETC, works without any issues.
Below is a screenshot of the AAM settings on SharePoint Central Administration (CA).
https://nvurrw.dm1.livefilestore.com/y2pSt3Q0PTUU1-RSLYio5gt8SXDnAQjxVBw1Ww43M63SqJzLfu-M5QD5dvzkhNR3iqjsOCovtVNIzlB0YpFbooMs3CgUt4xDvuX_EjHGBq1fUk/Capture3.PNG?psid=1
Below is a screenshot of the IIS bindings we have currently setup.
https://nvurrw.dm1.livefilestore.com/y2piUvW83Ro0MKN3l9fKWTbfBE-NjeIA92Pg3FkagoHgE3-jf5kPwAMWXJA0QpKjzBWZD9L3-bnS7PjQkls77k-RkP4KbFmRPonILJiyJsJZ8I/Capture6.PNG?psid=1
Below are the screenshots of accessing the same site using different URLS
https://nvurrw.dm2304.livefilestore.com/y2p4BHzxANJs37fkqwbyeo_jloz8ncsuVV2VRUW84JpRFZczb-6gLoga8XA0BVd-8ytk7ks_0psJLJyHJ98v-LNtGjGMywt50k-mu9hm5nmSVw/Capture1a.png?psid=1
https://nvurrw.dm2301.livefilestore.com/y2pPlDYWVYZKEi-qu6gbr86YUI1VqoIgDGKItrPdkXE5qHkMUMOi-GzXly3nmhkW1vU-FnjeLjRSDS6_VrxP0eN1i89uZECM3hRpZrbzQibkRE/Capture1b.png?psid=1
What is the problem?
If you type in domain.com and hit enter, thanks to most browser's autocomplete functionality, the URL automatically populates as http://domain.com/ and then you receive the below error.
https://nvurrw.dm2301.livefilestore.com/y2pQAcTgqLuXfJBR3Odb1NoEmGGsqQxxKob4iXkBAjb-7X1TXJ91sIfckUXjbKqXIMdOb-1vbisBLO5Rgq5fE5Xxs8Xs6HvKOH90LSrlR1bSIY/Capture2.png?psid=1
What caused this to happen?
Although there could be several reasons which caused this issue, the most common reason I have come across is the host header URL added when a web application was built. If you typed in a specific URL when building a web application, that URL automatically becomes the default binding in the IIS. Now you need to manually add other possible bindings to your web application.
How to resolve this issue?
Two changes need to be made, one in IIS Manager and the other in the Web.config file. If you have web front end servers then you need to make the changes there.
Changes in IIS Manager
Below are the steps to make the changes to sites in IIS Manager. As an added precaution I have even added a binding for the company's domain name just in case an external user happens to type that in aswell.
1. Go to IIS Manager>Select the site of your choice and then click on Bindings as shown below.
2. Add the bindings of your choice. Below are the bindings that will be added.
a. http://sp2013test.contoso.com/ (This was originally added when the web application was made)
b. http://www.domain.com/ (This was added for AAM)
c. http://contoso.com/ (This is the new binding for re-direct)
d. http://domain.com/ (This is the new binding for re-direct)
Below is a screenshot of all the bindings add to the site on IIS Manager. As a side note, if you have several web front end (WFE) servers then its best to pick the IP address when you are adding bindings.
https://nvurrw.dm2303.livefilestore.com/y2ptloH9Jf8sl3q6n0hmKjhYz7yCssqGujqaUNLm9f_Be7ikaCvqFAQdjLDHCQxncZ7tVo9pDqejG3qGD2MCcxHZFYjhPIi3GH9xYQzwyU7i2c/Capture7.PNG?psid=1
Adding the above binding itself isn't enough because if you try accessing the site with http://contoso.com/ and http://domain.com/ you will get the below error. Below are screenshots
https://nvurrw.dm2303.livefilestore.com/y2pO-J3dleDxaGLtt4johK1_wTb0rsAS16KjV2Lzqp_MUcg1Z1kH9LcSZ2HuU--Yd20anRtyGZY8zw661k4Mexqw-f0YlYFdc4UW45jsaPx3-Y/Capture8.PNG?psid=1
https://nvurrw.dm2303.livefilestore.com/y2pGeYt3sVeOpSrW--3utSJ94U1k1drc0kFnW6o86aIBdYUcK86l-FwfYt73LRFysPiZOZtqInyTqElVjX0DB0sAMkrYg2RFlFrUa1vFCyMuKc/Capture9.PNG?psid=1
Changes in Web.Config file.
***Make a copy of the web.config file and save it before making any changes to it.
***You can either do a URL rewrite directly on the Web.config file or download and install the URL Rewrite 2.0 extension from http://www.iis.net/downloads/microsoft/url-rewrite. This installation does not require a reboot. Below is the added IIS feature once you have the URL Rewrite installed.
https://nvurrw.dm1.livefilestore.com/y2pa5vw9sND8K8t7G44OgFF8w24_wHJ1dpmMJp9C5xwn9-qVUUqK8AcJvvDllbf_9jnk92-p23xW7RTZS_ulFi-EwvMNEdG2s6ra5bXsgjViwk/Capture10.PNG?psid=1
In this article I am going to show you how to make changes to the Web.config file and URL Rewrite.
Make changes directly on the Web.config file
In the below screenshot I have provided the location where the web.config file is located for this web application.
https://nvurrw.dm2301.livefilestore.com/y2p5cI0wY4KRPLsOhLFNPjWha9j0HGs0yDI5Ec2VNW0UmKeGAaMyB4RbdQCzppfuw1fRBYQeYWE_pUi2ixidERZD6IxcM0C8jZZmT9bkUs8izw/Capture11.PNG?psid=1
You can use notepad to open and make changes to the web.config file.
Look for the <handlers>….</handlers> code in the config file. If your web.config file hasn't been edited before then it should look something like this.
https://nvurrw.dm2301.livefilestore.com/y2pABpRTNgOzaClft_98SJeq8do6xLjQPiUgO-0-8w2xdptNhujmcSuFjX0NjFU46iuXkd3jje-UlySWKuWdBJAtrHWIoNvwCs0ZoxrxInsANE/Capture12.PNG?psid=1
Right below the </handlers> added the below code.
<rewrite>
<rules>
<rule name="RedirectSiteURLRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\domain\com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.com/%7BR:1}" />
</rule>
</rules>
</rewrite>
Make changes using URL Rewrite.
Double click on URL Rewrite
https://nvurrw.dm2303.livefilestore.com/y2pux2NGy8zODRR6aZ0HqRpFmMgfYBIKl1bYu_MpVZhzn4KdaNO1kli55vm5iPH-M32sVdLJjZP_qXMo33YECtDmVIHcj2sEY9FxxS6s-tUtf8/Capture13.PNG?psid=1
On the top right under Actions click on 'Add Rule(s)…'
https://nvurrw.dm2301.livefilestore.com/y2pglf9Dd0chy87mfZbuF6DxUfb2mkgQ9CoKwc8lokfvwpsFvruGYyaG28dcBRy8E28mrPDLiG8a5wk5FJJSNL1NDdxhtjT_nuSQ01Cm9YgmaI/Capture14.PNG?psid=1
Select 'Canonical domain name'
https://nvurrw.dm2303.livefilestore.com/y2pn31GS0Dj8pPPQPLYg16UbvfSJQk-uO09FF4HCe4DG8TNMikJUiPkAc9arYuPwx12hlX_pN4VUwVyoBbgv2f1NYR61iUNAlZDToO2-xawWPU/Capture16.PNG?psid=1
Choose the URL that you want all the end users to be re-directed to. In my case it was www.domain.com. Click OK.
https://nvurrw.dm2301.livefilestore.com/y2pge6T0cr1KKmkMzrk3J9pHYshmKblm6P6BvI0HsH-gy25RlTThYYSNHb20JhgOtvytnvCGefl7HhM0t3xI1NLI8C7xjs7SK6qqfSfgVbjDaI/Capture17.PNG?psid=1
You will be redirected back to URL Rewrite and below is what you should see.
https://nvurrw.dm2304.livefilestore.com/y2pajuM3xxlux16zJ6bCxhpaSok8hG1fMwHfLJH-0G_CbGAJxGcLo4GbWKZYVAkgRkC-9tZRw3sxyGHos1JuCx9b7Gzl68Wo96CB7LOBYEwulk/Capture18.PNG?psid=1
We are now going to test it. Double-click the rule you just created and click on 'Test pattern…' as shown below
https://nvurrw.dm2304.livefilestore.com/y2pT4EmIShjOoOA-QaxgiG7-UTYF7cIkGubv_FzXwRijPeK-RFvbEBeRHmhr8_-ZrZRK0qiGkvNYrYrxFXpLwWDO3_z067qlVjdEzXGO1B9MO8/Capture19.PNG?psid=1
In 'Input data to test:' type in one of the bindings you added. I my case I typed in contoso.com. Click on 'Test' If the test is successfully then below are the results you should see.
https://nvurrw.dm2301.livefilestore.com/y2piF6okW1r5maqgiygtRQhBeiWM8pcL0QNl7rOhn010nMUDpk5eq3Jz_w8XEGdcu9lCvDs3J61FdeWw-zWIVPxoznFvqOHjUnIYX327JM0izs/Capture20.PNG?psid=1
Below is the results for testing domain.com
https://nvurrw.dm2301.livefilestore.com/y2pYrizpqrCR64b6oiJWdI5RqdKSXDtqzuLbNp0yDDwnhfjX4Aa2hd-LYWaUcK8VP2cT2EnIK_vqX2PGmAyzFoIHUdl0ooKwgKgRuragCDyZrU/Capture21.PNG?psid=1
Testing
Once you have made changes using the web.config file or using URL rewrite, open a browser and try accessing both http://domain.com/ and http://contoso.com/.
Even thought the URL on the browser is http://domain.com/, the authentication windows shows www.domain.com. Below is a screenshot.
https://nvurrw.dm2302.livefilestore.com/y2pE_Pynii7MQKVxOs_RH6pT0IUuV78fOmdD7ZURhrBvu6oCta2S0xR3EM-MzxkT_OxkwxrlF0cCOcfgdnTqgq9oZE-DAIz4NNpmQTWLAq27rU/Capture22.PNG?psid=1
The same for http://contoso.com/
https://nvurrw.dm2303.livefilestore.com/y2p1Wqi1IinCMr2nMevYbC24gGO-5mMf5GwrGNa2thJAwHPmUIXcVkuhbt5I0WsTXjtzWDzgJcOyZlQXSDs8__89nIX7wbo06uO8NXkt7AYy2c/Capture23.PNG?psid=1
After you have typed in the username and password, below is the URL on the browser i.e. the URL we want the end users to use.
https://nvurrw.dm2304.livefilestore.com/y2pPlDYWVYZKEi-qu6gbr86YUI1VqoIgDGKItrPdkXE5qHkMUMOi-GzXly3nmhkW1vU-FnjeLjRSDS6_VrxP0eN1i89uZECM3hRpZrbzQibkRE/Capture1b.png?psid=1
Conclusion
The steps described above are simple and straight forward, however, it is absolutely imperative that you do not miss a step.
If you have already setup all your taxonomy terms and search using a specific URL then I highly recommend using this re-direct approach. Its easier to force the end users to access the URL you want them to than rebuilt the configuration.