htaccess 中的 API 或模板文件夹重定向

发布于 2025-01-12 09:07:57 字数 793 浏览 1 评论 0原文

我的目录结构如下:

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2025-01-19 09:07:57
RewriteCond %{REQUEST_URI} ^$
重写规则。模板/index.html [QSA]

RewriteRule 模式(即.)不必要匹配除文档根之外的所有内容。并且 REQUEST_URI 服务器变量始终以斜杠开头,因此条件无论如何都不会匹配。因此,基本上,该规则什么都不做。

如果只重写根,那么您只需要在 .htaccess 中添加一个规则:

RewriteRule ^$ template/index.html [L]

理想情况下,这将在您现有的重写之前进行。 API,尽管严格来说这并不重要,因为其他重写不会重写根目录(由于限制条件)。

# 将每个请求推送到 api.php
RewriteRule ^(.*)$ api.php [QSA]

是否应该将所有内容真正发送到api.php,或者只是以/api/开头的URL,如您的示例中所示?至少,您应该使用 + 量词,而不是 *,因为您想要排除对文档根的请求(目前,第三个条件是阻止对文档根目录被重写)。

请注意,如果您的 URL 格式为 /api/someendpoint 并且您要重写为 /api.php,那么您需要确保在顶部禁用 MultiViews。 .htaccess 文件:

Options -MultiViews
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]

The RewriteRule pattern (ie. .) unnecessarily matches everything except the document root. And the REQUEST_URI server variable always starts with a slash, so the condition will never match anyway. So, basically, the rule is doing nothing

To rewrite just the root then you just need a single rule in .htaccess:

RewriteRule ^$ template/index.html [L]

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).

# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]

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:

Options -MultiViews
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文