Home > Magento Tutorials > Magento URL Rewrites on a Windows Server

Magento URL Rewrites on a Windows Server

Posted on: 25th Aug 2010 By: Luci Smethurst 3 Comments

For the record, I don’t recommend running Magento on a Windows server – I have nothing against IIS, but Magento was really built for Linux servers. Things like URL rewrites become needlessly difficult when they could be as simple as changing a dropdown box in the backend to “Yes”…

However, if you or a client is running Magento on a Windows server – and there is no option to transfer to Linux – and you would like to get your URL rewrites working, would like to set the default page to index.php, remove index.php from URLs and get the site redirecting from non-www to www and from index.php to the root then todays post on the Magento blog is for you!

First – and this is required – you must get in contact with whoever is looking after of your server. You need to have a URL Rewrite Module like Helicon installed.

Next is your web.config file – it works similar to a .htaccess, except that a Magento website on a Windows server will definitly listen to it.

  1. Create a new file in your favourite text/html editor. Anything from Notepad to Dreamweaver will do.
  2. Save your file as web.config.
  3. Enter the code below:
<configuration>

<system.webServer>

<defaultDocument>
<files>
<add value="index.php" />
</files>
</defaultDocument>

<rewrite>
<rules>
<rule name="Redirect domain.co.uk to www.domain.co.uk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.co.uk" />
</conditions>
<action type="Redirect" url="http://www.domain.co.uk/{R:0}" />
</rule>

<rule name="redirect to root">
<match url="^index\.php$" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="^$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.domain.co.uk/" />
</rule>

<rule name="Remove index.php from URLs" stopProcessing="true">
<match url=".*" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^/(media|skin|js)/" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>

</system.webServer>

</configuration>

Now, I’ll try and explain everything you can see here – it looks a feels a lot like XML. To begin with you open up configuration and system.webServer tag’s, which tells a Windows server that you’re editing the configuration, and the the latter applies specifically to IIS7.

<defaultDocument>

The defaultDocument specifies the default page to load should no page be specified. In this case index.php will load.

<rewrite>, <rules>

The rewrite and rules elements are quite self explanatory – all child single rules within this are related to rewriting URLs.

Non-www to www

You can name each of your rules to make the code more user friendly by using the name=”" area, and then again patternSyntax and stopProcessing are self explanatory. Having an asterisk withing the <match url=”"> catches all the URLs and then <conditions> specifies the conditions of the rule – ie the domain you want it to apply to.

<action> then specifies what you want to happen next – in this case “Redirect” under type=”" and your domain (with www.) in url=”". You can also specify a redirectType – I’ve chosen Permanent here.

Redirect to Root (remove index.php)

<match> URL here should be set to the default file name that you would like removing – in Magento’s case this would be index.php. For example if users navigate to www.domain.co.uk/index.php, with this rule in place they would automatically redirect to www.domain.co.uk. Again you set <conditions> and then an <action> – again a Permanent Redirect and the destination URL.

Remove index.php from URLs

A common problem when you turn on URL rewrites in Magento that’s running on a Windows server is that your URL rewrites happen – but the links are broken. A little annoying. This rule fixes this issue!

<match> should be set to all URLs after “.” and then <conditions> sets what you want it to apply to. <action type=”"> is this time set to Rewrite (rather than redirect – that’s already happening) and the url should be set to index.php.

Then hey presto! A working Magento site (hopefully) – URL writes and all, which is a must really if you are interested in magento seo. Thanks for visiting our blog at ecommerce web design!

3 Responses to “ Magento URL Rewrites on a Windows Server ”

  1. Matt
    #1 | 2nd December 2010

    Awesome. Spent 2 nights trying to get rewrites to work for magento and this helped a lot. One thing. I installed the freebie url rewrite mod and the default document section was giving 500 errors. After I took that out, everything worked!! Thanks.

  2. Magento
    #2 | 8th June 2011

    Yes, magento on windows is very slowly!

  3. chintan
    #3 | 24th September 2011

    i installed a magento from web server to localhost
    it shows me the home page but when i try to access the other links it gives page not found error!!!
    and its working well on web server
    and i also found that when i add “index.php” in the url manually it opens the link
    does anyone know how to get rid of this????
    thank u in advance

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.