Andrew Gunn's Blog
Tales of a half pint...
Thursday, 18 February 2016
Wednesday, 5 August 2009
Catch-all redirect in Apache
Assume you have a website running on an Apache server and the URL is http://mydomain.com/. What if you want to dominate the UK market too!? You'd probably buy mydomain.co.uk and point it to the same server. This is bad! It causes something called Canonicalization - web content that has more than one possible URL. This can lead to massive SEO penalties so my advice is avoid it at all costs.
Instead, have a single URL that serves the content and defer anything else. In this scenario, mycompany.com would be the master and the rest (e.g. mycompany.co.uk) would return a 301 redirect (not a 302). It's also worth mentioning that http://www.mycompany.com/ and http://mycompany.com/ are different (with and without the "www." prefix). If they both serve the same content, Canonicalization will occur. Choose one and stick with it.
The following can be put inside the httpd.conf file to allow a single URL access to a website and redirect the others:
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www
</VirtualHost>
<VirtualHost *:80>
ServerName default
ServerAlias *
Redirect 301 / http://mydomain.com/
</VirtualHost>
I'll also do a follow-up post to show how to do the same in IIS.
Wednesday, 22 July 2009
Wednesday, 15 July 2009
SQL - Dynamic Order By clause
Have you ever wanted a stored procedure to order it's results dynamically? The ORDER BY clause is the place to start but normally relates to a series of columns that's hard-coded. If you want this clause to change depending on some input, you can adapt the following SQL. It contains a switch statement that compares the value of a parameter against pre-defined string column names. When a match is found, the respective clause is inserted. You can also specify the direction (ascending/descending) which doubles the amount of case options.
declare @orderBy nvarchar(max)
set @orderBy = 'columnName'
-- 0 Descending
-- 1 Acsending
declare @orderByDirection int
set @orderByDirection = 0
select *
from tableName
order by
case when @orderBy = 'columnName' and @orderByDirection = 0
then columnName end desc,
case when @orderBy = 'columnName' and @orderByDirection = 1
then columnName end
Monday, 13 July 2009
ASP.NET MVC - UserControls and IPrincipal
This is my first post on ASP.NET MVC so I'm gonna keep it short and sweet (I'm sure they'll be plenty more to come :-D).
On a MVC View Page, you can access the user that has been previously authenticated with ease:
<%= User.Identity.Name %>
Unfortunately, you can't do the same in an MVC View User Control because they don't have a property called User. Instead, you have to use the ViewContext property which is available on any MVC View:
<%= ViewContext.HttpContext.User.Identity.Name %>
Friday, 20 February 2009
To serialize, or not to serialize - that is the question
When serializing objects, you sometimes want to exclude properties that don't meet a certain criteria. This validation could be a simple check for default/null values or a more complicated scenario.
This example shows how to prevent a DateTime property being serialized if it hasn't been set, meaning it's value will be DateTime.MinValue. You'll notice there is the property for date of birth and a method called ShouldSerializeDateOfBirth that has no parameters and returns a boolean value. If this returns true, serialize, and vice versa. How does it get called I hear you ask. I don't actually know exactly but the key to invoking is in the name - make sure it starts with "ShouldSerialize" followed by the name of the property (in this case, "DateOfBirth").
public bool ShouldSerializeDateOfBirth() { return DateOfBirth != DateTime.MinValue; }
[XmlElement("dateofbirth")]
public DateTime DateOfBirth { get; set; }
Monday, 9 February 2009
How do I create the ASPState database?
After installing the Microsoft .NET framework 2.0, launch a command window and run:
aspnet_regsql.exe -ssadd -sstype p -S {Server} -U {Username} -P {Password}