This content has moved - please find it at https://devblog.cyotek.com.

Although these pages remain accessible, some content may not display correctly in future as the new blog evolves.

Visit https://devblog.cyotek.com.

Using the MantisBT REST API when hosted on IIS

I'm currently in the process of moving our hosting services from one provider to another; although some parts of cyotek.com infrastructure runs on Microsoft Azure, a fair chunk uses more traditional hosting. Our MantisBT instance is one such service that I recently migrated.

Previously the instance was hosted on Linux, now it's on Windows. The initial migration seemed to have gone well and so I'd moved onto the next sub-domain on the list.

This morning however I noticed the error logs were listing that cyotek.com wasn't able to display product road maps. On testing, I was getting 404 responses for any calls to the REST API on the migrated MantisBT instance.

Apache Redirects

The REST API in MantisBT makes use of Apache's mod_rewrite module to rewrite any URI to /api/rest/ to /api/rest/index.php using the following rules in .htaccess

# Based on Slim Framework recommendation @ http://docs.slimframework.com/routing/rewrite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Of course, IIS doesn't support .htaccess files and so these rewrites never occur, hence the 404.

Redirecting in IIS

IIS has its own version of mod_rewrite, the URL Rewrite module, although it isn't installed by default. You can find how to install this module in a previous post.

Once installed, you can add rewrite rules to web.config either via the IIS Manager GUI, or by directly editing the configuration - this post will cover both approaches.

Importing .htaccess rules

I briefly spoke about manually modifying web.config to add rewrite rules in our post on Redirecting to HTTPS when using IIS behind a load balancer. This time I'm going to describe how to import the rewrite rules of a .htaccess file directly into IIS.

  • Firstly, open IIS manager and then navigate to the api/rest folder of your MantisBT installation

  • Next open the URL Rewrite section.

  • Click the Import Rules... option in the Actions sidebar.

  • In the Configuration file field, select the .htaccess file located in the local api/rest folder and then click Import.

  • Verify that the Rewrite rules field displays the appropriate redirect and that Converted Rules indicates that the rule was successfully imported.

    Importing .htaccess rules into IIS

  • In the Actions sidebar, click Apply to save the changes, then click Back to Rules to return to to the main URL Rewrite section listing the imported rule.

    The imported configuration

That should be all that is required; the REST API calls should now rewrite and succeed. To quickly test that all is well, open up http://yourinstantURL/api/rest/projects/ in your browser - if you get a 401 error, then the redirect is working correctly.

Manually updating the configuration

If your instance is hosted remotely you might not have direct access to IIS Manager or be able to remotely connect to it. Below is the complete web.config file used to enable redirects, simply copy it to the /api/rest folder of your MantisBT instance.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="API Redirect" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <!--# Based on Slim Framework recommendation @ http://docs.slimframework.com/routing/rewrite/-->
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" appendQueryString="true" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Update History

  • 2018-03-30 - First published
  • 2020-11-22 - Updated formatting

About The Author

Gravatar

The founder of Cyotek, Richard enjoys creating new blog content for the site. Much more though, he likes to develop programs, and can often found writing reams of code. A long term gamer, he has aspirations in one day creating an epic video game. Until that time, he is mostly content with adding new bugs to WebCopy and the other Cyotek products.

Leave a Comment

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Styling with Markdown is supported