Using the MantisBT REST API when hosted on IIS
A short post on getting the MantisBT REST API to work successfully in Internet Information Services on Windows.
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.
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
Of course, IIS doesn't support .htaccess
files and so these
rewrites never occur, hence the 404
.
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.
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.
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.
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.
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.
Like what you're reading? Perhaps you like to buy us a coffee?