HTACCESS:仅当该文件存在时,将URL重写为文件,但带有正则捕获的组

发布于 2025-01-21 23:20:44 字数 1093 浏览 3 评论 0原文

我有一个我在PHP(Codeigniter Framework)中建立的CMS。我在思考为什么每当PHP都必须处理所有代码时,才能使用页面响应。因此,相反,我将创建完整的HTML页面,并在用户要求它们时为它们提供服务。

这就是为什么我只有在该文件存在的情况下才需要将URL重写为特定文件的原因。为此,我需要使用正则捕获的组,因为我想在该文件夹和子文件夹中为所有文件构建此文件。

例如,我想在%{document_root}/out上放一堆HTML页面,并希望通过重写规则直接访问它们。

例如,如果URL是这样的:

htaaatps://www.dsaoidashd.com/services/development-of-desktop-applications

我想查看以下位置:

%{DOCUMENT_ROOT}/out/services

对于

development-of-desktop-applications.html 

并且存在,我想将其退还给客户端浏览器而不会继续。

当然,如果我有更多这样的级别:

htaaatps://www.dsaoidashd.com/services/development/of-desktop-applications

那么我需要检查此位置:

%{DOCUMENT_ROOT}/out/services/development

用于

of-desktop-applications.html 

文件并将其返回到调用浏览器。

我尝试了

我知道我必须使用RewriteCond检查文件是否存在,但是如何将其传递给RewriteCond Regex捕获的组,以便我可以根据提供的URL检查它们?

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/out/$1.html -f
RewriteRule ^.*$ /$1.html [L]

I have a CMS that I've built myself in PHP (Codeigniter framework). I was thinking why every time PHP has to process all code just that to respond with a page. So instead I will create the complete HTML pages and serve them when a user asks for them.

That is why I need to rewrite an URL to a specific file only if that file exists. For this, I need to use regex captured groups because I want to build this for all files in that folder and subfolders.

For example, I want to put a bunch of HTML pages on %{DOCUMENT_ROOT}/out and want to access them directly with rewrite rules.

For example, if the URL is like this:

htaaatps://www.dsaoidashd.com/services/development-of-desktop-applications

I want to look at the following location:

%{DOCUMENT_ROOT}/out/services

for

development-of-desktop-applications.html 

and if it exists, I want to return it to the client browser without continuing.

And of course, if I have yet more levels like this:

htaaatps://www.dsaoidashd.com/services/development/of-desktop-applications

then I need to check this location:

%{DOCUMENT_ROOT}/out/services/development

for

of-desktop-applications.html 

file and return it to the calling browser.

I tried

I know that I have to use RewriteCond to check if the file exists but how to pass it to the RewriteCond regex captured groups so I can check them based on the URL provided?

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/out/$1.html -f
RewriteRule ^.*$ /$1.html [L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

罪歌 2025-01-28 23:20:44

您的rewriteCond几乎是正确的,但是您必须在重新写入的组中捕获$ 1,您的目标需要为out/$ 1 .html

您可以在网站root .htaccess中使用此重写规则:

RewriteEngine On

# To internally forward /dir/file to /out/dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/out/$1.html -f
RewriteRule ^(.+?)/?$ out/$1.html [L]

Your RewriteCond is almost correct but you have to capture $1 in a group in RewriteRule and also your target needs to be out/$1.html.

You can use this rewrite rule in your site root .htaccess:

RewriteEngine On

# To internally forward /dir/file to /out/dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/out/$1.html -f
RewriteRule ^(.+?)/?$ out/$1.html [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文