.htaccess 表示 PDF URL

发布于 2025-01-01 13:15:43 字数 509 浏览 3 评论 0原文

我有一个名为 /pdf/ 的结构,其中存储生成的 PDF。我正在尝试使用 .htaccess 将访问者路由到 PDF 文件夹。

我的 .htaccess 文件中有此规则:

RewriteEngine On
RewriteBase /
RewriteRule (.*) index.php?page=$1.html
RewriteRule ??? for pdf??

(注意 ??? 标记...)

当我尝试访问网址 /manual_chap_1 它正在工作。但是当我尝试 /pdf/manual_chap_1 时,它似乎不起作用。这是我看到的一些输出:

$_GET[page] => index.php.html

我希望能够生成 PDF 文件,如果请求的位置不存在 PDF 文件,然后,我希望它加载 HTML 页面。

I have a structure called /pdf/ where generated PDFs are stored. I am trying to use .htaccess to route visitors to the PDF folder.

I have this rule in my .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule (.*) index.php?page=$1.html
RewriteRule ??? for pdf??

(Note the ??? marks...)

When I try to access the url /manual_chap_1 it is working. But when I try /pdf/manual_chap_1 it doesn't seem to be working. Here is some output I see:

$_GET[page] => index.php.html

I want to be able to generate the PDF file, if the PDF file does not exist in the requested location, and then, I want it to load an HTML page.

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

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

发布评论

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

评论(1

尹雨沫 2025-01-08 13:15:43

试试这个:

RewriteEngine On
RewriteBase /

RewriteRule ^pdf/([a-z0-9\-_\.]+)$ pdf.php?file=$1 [L,NC,QSA]

RewriteCond %{REQUEST_URI} !^/pdf
RewriteRule ^([a-z0-9\-_\.]+)/?$ index.php?page=$1.html [L,NC,QSA]

L: [L] 标志导致 mod_rewrite 停止处理规则集。在大多数情况下,这意味着如果规则匹配,则不会处理更多规则。这对应于 Perl 中的最后一个命令,或 C 中的 break 命令。使用此标志指示应立即应用当前规则,而不考虑进一步的规则。

QSA:当替换 URI 包含查询字符串时,RewriteRule 的默认行为是丢弃现有查询字符串,并将其替换为新生成的查询字符串。使用 [QSA] 标志会导致查询字符串被组合。

NC:使用[NC]标志会导致RewriteRule以不区分大小写的方式进行匹配。也就是说,它不关心匹配的 URI 中字母是大写还是小写。

此外,我建议使用破折号而不是下划线。

Try this:

RewriteEngine On
RewriteBase /

RewriteRule ^pdf/([a-z0-9\-_\.]+)$ pdf.php?file=$1 [L,NC,QSA]

RewriteCond %{REQUEST_URI} !^/pdf
RewriteRule ^([a-z0-9\-_\.]+)/?$ index.php?page=$1.html [L,NC,QSA]

L: The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

QSA : When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

NC: Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.

Also, I would recommend to use dashes instead of underscore.

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