htaccess:仅当该文件存在时才将 URL 重写到该文件,但使用正则表达式捕获组
htaccess: rewrite URL to a file only if that file exists, but with regex captured groups
我有自己在 PHP(Codeigniter 框架)中构建的 CMS。我在想为什么每次 PHP 都必须处理所有代码才能响应页面。因此,我将创建完整的 HTML 页面并在用户需要时提供它们。
这就是为什么我只需要将 URL 重写到特定文件,前提是该文件存在。为此,我需要使用正则表达式捕获的组,因为我想为该文件夹和子文件夹中的所有文件构建它。
比如我想把一堆HTML页放在%{DOCUMENT_ROOT}/out上,想直接用rewrite规则访问。
例如,如果 URL 是这样的:
htaaatps://www.dsaoidashd.com/services/development-of-desktop-applications
我想看看以下位置:
%{DOCUMENT_ROOT}/out/services
为
development-of-desktop-applications.html
如果它存在,我想return它到客户端浏览器而不继续。
当然,如果我还有更多这样的关卡:
htaaatps://www.dsaoidashd.com/services/development/of-desktop-applications
然后我需要检查这个位置:
%{DOCUMENT_ROOT}/out/services/development
为
of-desktop-applications.html
文件并return它到调用浏览器。
我试过了
我知道我必须使用 RewriteCond 来检查文件是否存在,但是如何将它传递给 RewriteCond 正则表达式捕获的组,以便我可以根据提供的 URL 检查它们?
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/out/.html -f
RewriteRule ^.*$ /.html [L]
你的 RewriteCond
几乎是正确的,但你必须在 RewriteRule
中的一组中捕获 </code>,并且你的目标需要是 <code>out/.html
.
您可以在站点根目录 .htaccess 中使用此重写规则:
RewriteEngine On
# To internally forward /dir/file to /out/dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/out/.html -f
RewriteRule ^(.+?)/?$ out/.html [L]
我有自己在 PHP(Codeigniter 框架)中构建的 CMS。我在想为什么每次 PHP 都必须处理所有代码才能响应页面。因此,我将创建完整的 HTML 页面并在用户需要时提供它们。
这就是为什么我只需要将 URL 重写到特定文件,前提是该文件存在。为此,我需要使用正则表达式捕获的组,因为我想为该文件夹和子文件夹中的所有文件构建它。
比如我想把一堆HTML页放在%{DOCUMENT_ROOT}/out上,想直接用rewrite规则访问。
例如,如果 URL 是这样的:
htaaatps://www.dsaoidashd.com/services/development-of-desktop-applications
我想看看以下位置:
%{DOCUMENT_ROOT}/out/services
为
development-of-desktop-applications.html
如果它存在,我想return它到客户端浏览器而不继续。
当然,如果我还有更多这样的关卡:
htaaatps://www.dsaoidashd.com/services/development/of-desktop-applications
然后我需要检查这个位置:
%{DOCUMENT_ROOT}/out/services/development
为
of-desktop-applications.html
文件并return它到调用浏览器。
我试过了
我知道我必须使用 RewriteCond 来检查文件是否存在,但是如何将它传递给 RewriteCond 正则表达式捕获的组,以便我可以根据提供的 URL 检查它们?
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/out/.html -f
RewriteRule ^.*$ /.html [L]
你的 RewriteCond
几乎是正确的,但你必须在 RewriteRule
中的一组中捕获 </code>,并且你的目标需要是 <code>out/.html
.
您可以在站点根目录 .htaccess 中使用此重写规则:
RewriteEngine On
# To internally forward /dir/file to /out/dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/out/.html -f
RewriteRule ^(.+?)/?$ out/.html [L]