htaccess 中的 API 或模板文件夹重定向
我的目录结构如下:
.htaccess
api.php
template/index.html
当调用domain.com/api/someendpoint
时,请求被转发到api.php
。
# enable apache rewrite engine
RewriteEngine on
# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-d
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
现在,当调用domain.com
时,请求应该调用到模板目录中的index.html
:
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
这不起作用,directoryListening总是显示。
获取index.html
的正确方法是什么?
I have a directory structure as follows:
.htaccess
api.php
template/index.html
When calling domain.com/api/someendpoint
, the request is forwarded to api.php
.
# enable apache rewrite engine
RewriteEngine on
# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-d
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
now, when calling domain.com
, the request should be called to the index.html
in the template directory:
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
this does not work, the directoryListening is always displayed.
What is the correct way to get the index.html
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RewriteRule
模式(即.
)不必要匹配除文档根之外的所有内容。并且REQUEST_URI
服务器变量始终以斜杠开头,因此条件无论如何都不会匹配。因此,基本上,该规则什么都不做。如果只重写根,那么您只需要在
.htaccess
中添加一个规则:理想情况下,这将在您现有的重写之前进行。 API,尽管严格来说这并不重要,因为其他重写不会重写根目录(由于限制条件)。
是否应该将所有内容真正发送到
api.php
,或者只是以/api/
开头的URL,如您的示例中所示?至少,您应该使用+
量词,而不是*
,因为您想要排除对文档根的请求(目前,第三个条件是阻止对文档根目录被重写)。请注意,如果您的 URL 格式为
/api/someendpoint
并且您要重写为/api.php
,那么您需要确保在顶部禁用 MultiViews。.htaccess
文件:The
RewriteRule
pattern (ie..
) unnecessarily matches everything except the document root. And theREQUEST_URI
server variable always starts with a slash, so the condition will never match anyway. So, basically, the rule is doing nothingTo rewrite just the root then you just need a single rule in
.htaccess
:Ideally, this would go before your existing rewrites for your API, although it doesn't strictly matter since the other rewrites do not rewrite the root directory (because of the restrictive conditions).
Should everything really be sent to
api.php
, or perhaps just URLs that start/api/
, as in your example? At the very least, you should be using the+
quantifier, not*
, since you want to exclude requests for the document root (currently, the 3rd condition is blocking requests to the document root from being rewritten).Note that if your URLs are of the form
/api/someendpoint
and you are rewriting to/api.php
then you need to ensure that MultiViews is disabled at the top of the.htaccess
file: