I'm trying to develop a vuejs frontend site with wordpress as headless CMS.
so far everything is fine, but one thing worries me.
the frontend is served at domain.com/index.html so I renamed the wordpress root document index.php to _index.php. For reading the /wp-json/v2/ API I added
RewriteRule ^wp-json/(.*) /_index.php [L]
to the apache .htaccess file to redirect the call.
However, if I now also want to call the old json API with ?json=, the
RewriteRule ?json(*) /_index.php [L]
or
RewriteRule ^?json=(.*) /_index.php [L]
does not work.
how can I use both APIs, no idea why the rewriteRule doesn't work.
this is how mine looks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^wp-json/(.*) /_index.php [L]
#RewriteRule ^?json(*) /_index.php [L]
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
by the way: is there a way with the json API V2 to output the pages recursively as Parent/Child tree?
Why? If you are having issues with
index.php
taking priority when requesting the root directory then reset theDirectoryIndex
. For example:Now, only
index.html
will be checked for when requesting a directory. (It was probably set to something likeDirectoryIndex index.php index.html
previously, soindex.php
would take priority.)The
RewriteRule
pattern matches against the URL-path only, which notably excludes the query string. To match the query string you need to use a separate condition (RewriteCond
directive) and check against theQUERY_STRING
server variable.For example:
The regex
^$
ensures that only requests for the document root are matched and not any URL-path.^json=
matches any query string that startsjson=
. The(.*)
part is not required. TheQUERY_STRING
server variable does not include the?
delimiter itself.NB: The regex
?json(*)
(in your first example) is not valid and you'd expect to get a 500 response (if on Apache), since the regex would fail to compile.The
/
prefix on the substitution string is not required and should be avoided. (This now makes use of theRewriteBase
directive - which isn't actually required here anyway.)So, in summary, it would look like this, with a few minor tweaks: