HTACCESS:仅当该文件存在时,将URL重写为文件,但带有正则捕获的组
我有一个我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
rewriteCond
几乎是正确的,但是您必须在重新写入
的组中捕获$ 1
,您的目标需要为out/$ 1 .html
。您可以在网站root .htaccess中使用此重写规则:
Your
RewriteCond
is almost correct but you have to capture$1
in a group inRewriteRule
and also your target needs to beout/$1.html
.You can use this rewrite rule in your site root .htaccess: