How to redirect all API requests using .htaccess, while keeping asset requests intact?

TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the ‘root’ of the site.

So, I’ve spent way too many hours on this and trust me, I’ve dug through all of the resources for mod_rewrite. I guess I’m just not quite understanding and figured I’d ask on here.

Read More

What I want to do, in theory, seems simple. I’m building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.

Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the “base” of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).

I feel like I’m onto something here and am surprised I couldn’t find anything that directly tackles this issue.

What I’ve done now is renamed the index.php from WordPress to index-api.php and left it the same:

index-api.php:

<?php

define('WP_USE_THEMES', true);



/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();

.htaccess:

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]

</ifModule>

I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I’m close.

Any suggestions?

Would appreciate anyone’s help on this, it’s been really confusing!

Related posts

Leave a Reply

1 comment

  1. In the first place you’ll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We’ll also need to make sure that the second rule will not match what it rewrites, or we’ll end up with an infinite loop and an internal error.

    You can use the $1 in the first rule too, as I show below:

    RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
    
    RewriteCond %{REQUEST_URI} !^/site/dist/
    RewriteCond %{REQUEST_URI} !^/index-api.php
    RewriteRule (.*)$ site/dist/$1 [L]
    

    I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url’s.