我正在尝试使用React实现Wordpress REST API。现代惯例似乎是将API设置为域的子文件夹,即
https://sitename.com/api/v1
I want to be able to go to
https://sitename.com/api/v1/wp-admin
And be able to sign into wordpress and have it so the REST api is accessed like:
https://sitename.com/api/v1/wp-json/wp/v2/pages
我有这个文件结构:
里面api/
是WordPress的安装。如果我将api包含在公用文件夹中,并指向带有虚拟主机的build文件夹,则它会按预期运行,除了以下事实:在我构建React应用程序时,它会清除Wordpress安装并重新启动整个过程。
我尝试将虚拟主机设置为子文件夹,但它仍返回404。
编辑:要获取此运行的简单实例:
yarn create-react-app dir_name
cd dir_name
mkdir api
cd api
wp core download
# Add virtual host
# Generate SSL Cert
我想到的最好的方法是将符号链接附加到api/
目录,然后将其链接到build/
目录。
# Source->Destination
ln -s /var/www/site.com/public/api /var/www/site.com/build/
根据您的配置,您可能需要更改api/
组,然后再执行一次操作yarn build
,服务器才能访问Wordpress(例如添加插件)。
快速浏览:
ln -s /var/www/site.com/api /var/www/site.com/public/
yarn build/
sudo chown -R :www-data build/
编辑:我最近采用了使用apache虚拟主机的感觉更好的方法。
首先,我将根目录细分为frontend
和backend
目录。前端内部是的输出create-react-app
。里面backend
是api/wp
。我将Wordpress嵌套在一个api目录中,以防该项目进行扩展并包含多个api,它们都将被组合在一个地方。
这是目录结构
Inside the backend .htaccess is a redirect to the wordpress install
RewriteEngine On
RewriteBase /api
RewriteCond %{REQUEST_URI} !^/api/wp
RewriteRule (.*) wp/$1 [R=302,L]
Inside wordpress is a blank theme I made that redirects to the react app url, in this case, isma.test.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句