One of the least documented aspects of Umbraco has to be the in-built Examine search mechanism. I’m going to do a couple of simple blog post’s to go through the whole process of building search into your site one step at a time. This first post is aimed at setting up nice url’s for your search page.
One of the things I like to do when building a website is use nice url’s, for example my search results can be found at;
This is in contrast to how most sites clearly expose the query string like follows;
You will notice if you browse to these pages that both of these addresses produce the same result and in fact go to the exact same page. In Umbraco, this is achieved through the use of URL Rewriting.
First of all, make sure that the “umbracoUseDirectoryUrls” setting in your web.config is set to true. This is what makes url’s of pages appear in the format shown above instead of like this:
In order set up URL Rewriting, you first need to have created a page that will contain your search results (doesn’t need to be set up yet). I created a page called “Search Results” which translates to /search-results/. Now in order to set /search/ to point to /search-results/ and pass the search query as the “q” parameter and the page of the results as the “page” parameter, I add the following to my UrlRewriting.config:
Now let me break this down by attibute.
name: This ones quite self-explanatory, you need to give the rule a name.
virtualUrl: You see the ^~/search/ part of the url, any content after the ^ is the address that will be matched after the domain name.
Next you see elements inside a set of brackets. The values are Regex’s used to match the values in the requested url. They are separated by the / characters in the url like a normal url to split up elements with different meanings.
destinationUrl: If the request url matches the virtualUrl the elements in brackets become a variable that can be used in the destinationUrl identified by $1, $2 etc. (based on the order of appearance). Therefore, (.*) maps to $1 and ([0-9]+) maps to $2. The content in the brackets means:
.* matches zero or more of any character
[0-9]+ matches one or more numbers
ignoreCase: This is another self-explanatory one, this just allows us to ignore the case of the input values.
You should now have a your url’s set up so that you can perform a search by visiting a nice url in the format of /search/query/page/ like:
Next up is part 2, creating the search box.