I have used IIS's URL Rewrite module in several of my .net projects. It is a very neat module that gives a lot of URL rewrite/redirect functionality out-of-the-box. Namely, the module can do:
Example: /device/page.html should be accessible to the Internet as /devices/page.html, even though in Tridion, the page is under Structure Group "/device".
Enter IIS URL Rewrite. I set a rewrite rule in web.config that takes a URL starting with /devices/ and rewrites it server side to /device/ followed by the same sub-path and .html page name. The rule uses regular expressions and replacement groups:
Using the rule above a certain page under /device is now also available under /devices. From an SEO perspective, this is something to avoid. The same page should not be accessible on the same website under more than one URL. So the next rule takes care of that. It forces direct access to pages under /device to yield HTTP status 404.
There is one more issue left to deal with -- Tridion-resolved Component links to the page under /device. These links will be resolved to the actual URL of the page in Tridion, i.e. /device/page.html. But we want this URL to be exposed as /devices/page.html.
I chose to use URL Rewrite module to rewrite these links in the response body. So the rule below intercepts only anchor href links to the /device/ URL and rewrites them to /devices/. The rewrite occurs using regular expressions and replacement groups, but it is restricted to only those responses that have content mime-type text/html. This done simply for performance reasons, so we don't try accidentally to rewrite a binary response, for example.
- URL rewrite -- rewrite the URL path before request processing starts (similar to a server transfer);
- URL redirect -- redirects the client browser to a modified URL by sending back redirect HTTP status codes;
- Rewrite outgoing URLs in the response body;
Example: /device/page.html should be accessible to the Internet as /devices/page.html, even though in Tridion, the page is under Structure Group "/device".
Enter IIS URL Rewrite. I set a rewrite rule in web.config that takes a URL starting with /devices/ and rewrites it server side to /device/ followed by the same sub-path and .html page name. The rule uses regular expressions and replacement groups:
<system.webServer> <rewrite> <rules> <rule name="'devices' to 'device'" stopProcessing="true"> <match url="^devices/(.*\.html)$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="device/{R:1}" /> </rule> ...
Using the rule above a certain page under /device is now also available under /devices. From an SEO perspective, this is something to avoid. The same page should not be accessible on the same website under more than one URL. So the next rule takes care of that. It forces direct access to pages under /device to yield HTTP status 404.
<rule name="'device' yields 404" stopProcessing="true"> <match url="^device/.*\.html" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="CustomResponse" statusCode="404" statusReason="Page Not Found"
statusDescription="Page Not Found" /> </rule>
There is one more issue left to deal with -- Tridion-resolved Component links to the page under /device. These links will be resolved to the actual URL of the page in Tridion, i.e. /device/page.html. But we want this URL to be exposed as /devices/page.html.
I chose to use URL Rewrite module to rewrite these links in the response body. So the rule below intercepts only anchor href links to the /device/ URL and rewrites them to /devices/. The rewrite occurs using regular expressions and replacement groups, but it is restricted to only those responses that have content mime-type text/html. This done simply for performance reasons, so we don't try accidentally to rewrite a binary response, for example.
<outboundRules> <rule name="'device' to 'devices'" preCondition="IsHTML" enabled="true" stopProcessing="false"> <match filterByTags="A" pattern="^/device/(.*)$" /> <action type="Rewrite" value="/devices/{R:1}" /> <conditions logicalGrouping="MatchAny"/> </rule> <preConditions> <preCondition name="IsHTML" logicalGrouping="MatchAny"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> </preCondition> </preConditions> </outboundRules>
Redirect Directories to index.html
Take the following rule as a bonus example. It redirects the client browser to the /index.html in case the requested URL is to a directory. The rule is a bit more complex, so I'll explain the regular expression ^((|devices)[^\.]*)(\/)?$ :- ^(|devices) match URL path that starts either with nothing OR 'devices' -- the match on nothing is for home pages, which don't have a URL path at all;
- [^\.]* followed by zero or many characters that are not a dot (.) -- this indicates our request URL path is a directory (i.e. it doesn't contain an extension);
- (\/)?$ URL path ends with an optional forward slash (/);
<rule name="Redirect directories to /index.html" stopProcessing="true"> <match url="^((|devices)[^\.]*)(\/)?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> </conditions> <action type="Redirect" url="{R:1}/index.html" /> </rule>
Comments