IIS: Redirect HTTP to HTTPS

To cleanly redirect HTTP to HTTPS with IIS, first install the URL Rewrite module, and then add the following to your web.config. Also remember to have bindings defined for your site for both HTTPS(443) and HTTP(80).

        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />

Source: StackOverflow

SQL Server evaluation license expiration date

Running the following SQL should give you how many days left until your evaluation license runs out. Also tried adding a calculation of when that date is. Either way, beware of off-by-one errors…

sp_configure 'show advanced options', 1;

sp_configure 'Agent XPs', 1;

DECLARE @daysleft INT
DECLARE @instancename sysname
SELECT @instancename = CONVERT(sysname, SERVERPROPERTY('InstanceName'))
EXEC @daysleft = xp_qv '2715127595', @instancename
    DATEADD(dd, @daysleft, CONVERT(DATE, getdate())) 'Expiration date',
    @daysleft 'Days left'

Source: When will my SQL Server Evaluation Edition expire?

Shortcut to Resource Monitor in Windows 8.1

In Windows 7 there was a shortcut to Resource Monitor in the start menu, which meant you could launch it quickly through search. In Windows 8.1 this didn’t seem to be the case for some reason. Might have missed something… but found the shortcut for it on a Windows Server 2012 installation, and it worked just as nicely in Windows 8.1. So, note to self:

%windir%\system32\perfmon.exe /res

So after quickly making a shortcut to that in my menu, I’m once again back to launching it directly rather than through the button in Task Manager.

Change license key of Windows Server

Needed to install two Windows Server 2012 R2 instances today, but didn’t have a valid production license key yet and didn’t seem like there was a way to skip the key in the setup. Found you can remove a license key using a command, so worked around it using a temporary dev key from MSDN for the installation and then removed that key afterwards. For future self-reference, here’s how…

  1. Open elevated command prompt
  2. To remove key
    > slmgr -upk
  3. TO add new one
    > slmgr -ipk NEW-KEY or use regular activation process

Ignoring files and folders in Sublime Text

I love the file search (ctrl+shift+f) and quick navigate (ctrl+p) in Sublime Text. What can be annoying however is when these present you with library code in your project, files related to source control, binary files or minified files.

Since I keep forgetting how to clean that up, here’s a note to self (and others) on how to clean that up. Easy peasy.

  1. Save as project (if you haven’t already)
  2. Edit project file
  3. Add file_exclude_patterns and/or folder_exclude_patterns to the folders section.

Below is an example from one of my current projects which excludes the git repo folder, a cache folder, minified files and some binary files.

            "path": "/D/dev/www/sdaweb-ssl"

Apache: Redirect one domain to another

The following .htaccess redirects all traffic to a different domain and preserves the path.

RewriteEngine On
RewriteRule (.*) http://example.com/$1 [R=301,L]

The following redirects non-www to www version, also preserving the path.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

And hopefully I’ll remember I put this here the next time I’ve forgotten how to do this…

C#: Ordering by a list of ordered predicates

In a project at work we were going to merge a bunch of PDfs and Word documents into a single PDF. The ordering irrelevant except that certain files had to be before all the others. Solved it initially like this:

    var filesInOrder = GetFiles()
        .OrderByDescending(x => x.Filename.StartsWith("ProjectDescription_"))
        .ThenByDescending(x => x.Filename.StartsWith("Budget_"))
        .ThenByDescending(x => x.Filename.StartsWith("CV_"))

Found it a bit ugly though, and decided to ask a question about other ways on StackOverflow. Got several interesting answers and inspired by those and some further thinking I tried to make a generic solution myself, which I thought I could also blog here so I definitely know where to find it if I ever need it again…

My solution

public class OrderedPredicateComparer<T> : IComparer<T>
    private readonly Func<T, bool>[] ordinals;
    public OrderedPredicateComparer(IEnumerable<Func<T, bool>> predicates)
        ordinals = predicates.ToArray();

    public int Compare(T x, T y)
        return GetOrdinal(x) - GetOrdinal(y);

    private int GetOrdinal(T item)
        for (int i = 0; i < ordinals.Length; i++)
            if (ordinals[i](item))
                return i - ordinals.Length;
        return 0;

One issue here might be that the predicates will be called several times per item, which could be bad if working on a huge list. Haven’t really benchmarked it though so could be fine for all I know. For smaller uses it shouldn’t matter much either way. Very curious to know about ways to optimize this though, so do let me know in the comments below if you have any good ideas!

The nice thing about it being an IComparer is that you could push this into both OrderBy and ThenBy, and also use it in for example ordered dictionaries, priority queues, etc, and since it uses a list of fully generic predicates you could order things by pretty much anything with a yes/no answer :)


var ordering = new Func<string, bool>[]
        x => x.StartsWith("ProjectDescription_"),
        x => x.StartsWith("Budget_"),
        x => x.StartsWith("CV_"),

var files = GetFiles()
    .OrderBy(x => x.Filename, new OrderedPredicatesComparer<string>(ordering))

To make the final code even cleaner the ordering could be encapsulated in a sublcass like following, which is what I did in my actual code too:

public class MySpecificOrdering : OrderedPredicatesComparer<string>
    private static readonly Func<string, bool>[] order = new Func<string, bool>[]
            x => x.StartsWith("ProjectDescription_"),
            x => x.StartsWith("Budget_"),
            x => x.StartsWith("CV_"),

    public MySpecificOrdering() : base(order) {}

var files = GetFiles()
    .OrderBy(x => x.Filename, new MySpecificOrdering())

With a hint of Social Ineptitude