简体   繁体   English

如何使用.htaccess重定向所有API请求,同时保持资产请求完整无缺?

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

TL; 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. DR:如果要在URL中找到api我想打一下index-api.php文件,但是只需将所有其他请求都指向site/dist目录,就好像它是站点的“根”一样。

So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. 因此,我在此上花费了太多时间,并且相信我,我已经挖掘了mod_rewrite的所有资源。 I guess I'm just not quite understanding and figured I'd ask on here. 我想我只是不太了解,因此我想问一下。

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. 我正在使用Grunt构建单个页面应用程序(Angular App),并将其输出到WordPress安装的根目录。 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. WordPress安装只是使用WordPress JSON API插件提供了一个API,因此我希望网站的根目录能够打到我的Grunt目录(位于site/dist/index.html ),但是所有对siteurl.com/api请求点击index.php文件并正常进行。

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 (eg a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg ). 请记住,我在此site/dist目录中还有其他资产/图像,因此,理想情况下,如果对站点根目录的所有请求都只是使用此文件夹作为站点的“基础”(例如,对siteurl.com/images/testimage.jpg的请求),那将是很棒的siteurl.com/images/testimage.jpgsite/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: 我现在所做的是将WordPress的index.php重命名为index-api.php并保持不变:

index-api.php: 指数api.php:

<?php

define('WP_USE_THEMES', true);



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

.htaccess: 的.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. 有趣的是,如果我注释掉RewriteRule (.*)$ site/dist/index.html [L]的最后一行,则api请求可以按预期正常运行,所以我知道我已经关闭了。

Any suggestions? 有什么建议么?

Would appreciate anyone's help on this, it's been really confusing! 感谢任何人对此的帮助,这确实令人困惑!

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. 首先,您需要确保对/index-api.php请求不被第二条规则匹配和重写。 In the second rule you can use $1 . 在第二条规则中,可以使用$1 $1 will be replaced with whatever was matched in the first capture group. $1将替换为第一个捕获组中匹配的任何内容。 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: 您也可以在第一条规则中使用$1 ,如下所示:

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. 我建议阅读mod_rewrite 的文档 ,以更好地了解如何使用它以及在重写url时可以使用的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM