Pretty WordPress Permalinks on Azure

So I’m in the process of moving my company website to Azure and I’ve decided to create it using WordPress as I’ve enjoyed the experience of WordPress so far with this blog.

This blog is hosted for free by my good awesome friend Brandon (www.ganch.com) and so I haven’t asked any questions as to what platform it’s hosted on.  I’d assumed it’d be an IIS server but, as they say, if you assume something (insert terrible pun here).  So imagine my surprise when I popped down to the Permalinks section of my Azure hosted WordPress site and saw that my page links (note it’s page links only I’m referring to here folks, the blog posts were fine) could only be almost pretty.  Almost pretty?  WTF?  That won’t do because this site is presently the most important site in the world to me and I demand that it’s links be of the friendly variety.

A quick-ish Google later and I’d found the solution, and it makes complete sense.  It transpires that this blog is hosted on a Linux box which has Apache’s mod_rewrite module installed/configured/running/whatever.  As I’m working with an IIS server on Azure I need to create a rewrite rule to get the friendly/pretty urls that I want.

So how to do it?  Here’s how…

Azure Management Portal

Okay so I’m working on the assumption (assuming again!!!) that you’ve already gotten a WordPress site set up on Azure but if you haven’t then please read this article, then return back here.

From within your Azure Management Portal, go to Web Sites, click on the name of your site and from the overview, click on the Download the publish profile link:

Windows Azure download publish profile link

Once that’s downloaded it’s over to WebMatrix

WebMatrix

Open up (or download) WebMatrix then go to File | Open | Remote Site

Open a remote site from WebMatrix

From the resultant dialog, select Import publish profile:

Add remote site dialog

Choose the publish profile you downloaded a couple of steps previous and click Open.  You’ll then get to the Remote Site Settings dialog from where you should just click Save which will cause your remote site to be loaded into WebMatrix.

Okay, now it’s time to create that all imporant rewrite rule.

Web.Config

To get our rewrite rule in play we need to create a web.config file at the root of the WordPress site.

To do this, right click on the name of your site and select New File.

New File menu option

From the resultant popup click on the TXT file and change the name to web.config.  Click OK.

New File dialog

You should now have a file structure that looks like this with that all important web.config file:

WordPress file structure with web.config

Now (finally) for the rewrite rule.  Open up your web.config file and enter in the following code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Main Rule" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This rule is in place to try and match any URL that gets requested.  If a match is made then the content will be served up.  If a match isn’t made then the URL will be rewritten to Index.php.

Save the file which will save it remotely and go back into your WordPress administration site.

WordPress

For my site I want my blog pages to follow the pattern http://<domain>/products/%postname%/ meaning my pages will follow the pattern http://<domain>/%postname%/ so I’ll get nice friendly URLs like http://<domain>/about/.

So, from within the Permalinks Settings page I choose the Custom Structure option and enter in /products/%postname%/ then click on Save (I’m tired of taking screenshots).

Visiting my site now I can see that my links are indeed friendly and follow the pattern I want.

To be honest I’m a bit surprised that Microsoft haven’t included the rewrite option as part of the site creation process but maybe they will one day.  Until then I hope this post will be of use to some of you.

2 Comments Pretty WordPress Permalinks on Azure

Leave a Reply

Your email address will not be published. Required fields are marked *