Jay Gould

Redirecting API requests and post data using htaccess

February 07, 2019

I encountered a problem at work recently which led me to implement .htaccess redirects. This short post will run through the problem briefly, and then giving the redirect solution. Hopefully it will help someone else, and will definitely be a place for me to look back to find the solutions quickly in future.

Why I needed server redirects

My work colleague and I have been developing a website and mobile app over the last few weeks at work. The app sends request to the API of the staging site which is currently in development, as the staging site has new server side logic compared to the live site. The plan is for the mobile app and staging site to be pushed live to the public in the next few weeks, and this will involve changing the URL of the staging site to that of the live site.

When publishing the app and website, as long as they both go live at the same time, the app can point it’s API requests to the live site (because the staging site will be the live site when everything is live). However, as iOS app are required to be sent to Apple for approval, the app needs to be able to function in order to be tested before the staging site is pushed live. Until the website is published, the app will essentially be pointing to the wrong location which contains the wrong server side endpoints and functionality.

We discussed a few options for making this work, including setting the server address dynamically from the app (perhaps by doing an initial request to an independent location to get the correct server address), and replicating the staging functionality on the live server until the staging site is published. Both solutions seemed to be long winded, and time was not on our side.

The redirect solution using .htaccess file

The solution was a simple one in the end, although not so simple for me as I’m admittedly terrible as .htaccess configuration. The idea was to have the app send it’s API requests to the live site as it normally would, but the live server’s .htaccess file would redirect certain requests to the staging site.

The syntax is fairly simple:

RewriteRule ^some/folder/to/file.php$ http://stage.website.com/some/folder/to/file.php [L,R=301,NC,P]

The important part about this redirect rule is that the post data is preserved, which is required when POST requests are sent to the server.

It may also be required to redirect with GET requests:

RewriteCond %{QUERY_STRING} my-query-param=yay
RewriteRule ^some/folder/to/file.php$ http://stage.website.com/some/folder/to/file.php?my-query-param=yay [L,R=301,NC,P]

The query params must be defined separately in the RewriteCond and added again to the end of the redirect destination in the RewriteRule.

We implemented a separate redirect rule for each API endpoint we have from our app. This may not be the best way, but it’s what we settled on. If you know a better way please get in touch as I’d love to get this looking better!

The key points to syntax in these .htaccess rules are as follows:

  • The ^ (caret) matches the start of the URL.
  • The $ matches the end of the URL.
  • The [L] tells the server to stop processing rules after this one.
  • The [R=301] permanent redirect to new URL.
  • The [NC] rules are case sensitive.
  • The [P] grab the remote content via a proxy.

That’s it, thanks for reading! If you have anything to correct me on or improve, please get in touch.


Senior Engineer at Haven

© Jay Gould 2023, Built with love and tequila.