我希望所有具有字符wp-
作为第一个加载index.php
路径的请求。对于任何其他请求,我想加载index.html
。
如果请求转到https://example.com/wp-admin
,我想加载index.php
如果请求转到https://example.com/anything
,我想加载index.html
如果请求转到https://example.com/anything/wp-something
,我想加载index.html
原因是我在同一个Web服务器上提供单页应用程序(Angular(和WordPress。我希望Angular处理所有路由,除非第一个路径中有wp-
,这样我的编辑器就可以轻松访问WordPress界面。
这可能吗?不良做法?更好的解决方案?
我实际上是用我的一个插件Xo for Angular来实现这一点的,它提供了一种集成的方法,可以将Angular应用程序作为WordPress中的常规主题加载。
通过执行以下操作,将所有前端请求定向到单个模板(如Angular App(相对容易:
首先,您需要一个php文件,可能是主题中的index.php,以包含Angular应用程序的内容。
// index.php
<?php
echo file_get_contents(get_template_directory() . '/dist/index.html');
接下来,你可以放置一个过滤器,这样无论前端路径如何,WordPress都会呈现你的index.php,让Angular应用程序接管它。
// functions.php
<?php
add_filter('template_include', function() {
return get_template_directory() . '/index.php';
}, 1, 0);
希望这能奏效!