与pdf目录的htaccess重写规则混淆(Confusion with htaccess rewrite rule for pdf directory)

编程入门 行业动态 更新时间:2024-10-27 03:28:35
与pdf目录的htaccess重写规则混淆(Confusion with htaccess rewrite rule for pdf directory)

我不是htaccess专家。 我很少用它来阻止狡猾的蜘蛛和基本的CI路由来删除index.php。

但基本上,我试图将所有网址请求转换为:

https://www.domain.tld/invoice/ [1个或更多整数] / [字母数字字符串] .pdf

(例如https://www.domain.tld/invoice/674/9b3vpm3cy5nd8irm.pdf )

https://www.domain.tld/invoice/fetch/ [1个或更多整数] / [字母数字字符串] .pdf

(例如https://www.domain.tld/invoice/fetch/674/9b3vpm3cy5nd8irm.pdf )

到目前为止,我有 -

RewriteRule ^invoice/(.+)/(.+\.pdf)$ index.php?/invoice/fetch/$1 [L]

但它无法正常工作(毫不奇怪)并且对https://www.domain.tld/css/pdf.min.css的请求进行了修改

有人可以帮助正确构建这个重写规则吗? 我意识到这可以使用CI路由轻松完成,但我需要在htaccess中完成,因此这条规则至关重要。

I'm no htaccess expert. I've rarely used it for more than blocking dodgy spiders and the basic CI routing to remove index.php.

But basically, I'm trying to turn all requests for urls structured like:

https://www.domain.tld/invoice/[1 or more integers]/[alphanumeric string].pdf

(such as https://www.domain.tld/invoice/674/9b3vpm3cy5nd8irm.pdf) to

https://www.domain.tld/invoice/fetch/[1 or more integers]/[alphanumeric string].pdf

(such as https://www.domain.tld/invoice/fetch/674/9b3vpm3cy5nd8irm.pdf)

So far I've got -

RewriteRule ^invoice/(.+)/(.+\.pdf)$ index.php?/invoice/fetch/$1 [L]

but it's not working correctly (unsurprisingly) and mangling requests for https://www.domain.tld/css/pdf.min.css

Can someone please help construct this rewrite rule properly? I realize this can be done easily using CI routing but I need it done in htaccess so this rule is crucial.

最满意答案

重写斜杠/项目时没有简单的解决方案。 不过,您有一个简单的解决方法:

使用绝对路径的所有资源文件:

<link href="/css/pdf.min.css" rel="stylesheet" type="text/css">

而不是,例如,

<link href="../css/pdf.min.css" rel="stylesheet" type="text/css">

这需要对PHP脚本或ASP.NET进行重新编程。

你总是可以测试:

RewriteEngine On RewriteRule ^invoice/([^/]+)/(.+\.pdf)$ /index.php?/invoice/fetch/$1/$2 [L]

贪婪的正则表达式(。+)可能会导致一些问题。

I figured out the problem - another conflicting rewrite rule below was causing issues. I've looked further into regular expressions and have come up with a condition like so:

RewriteCond %{REQUEST_FILENAME} invoice/[0-9]+/[a-z0-9]+.pdf$ RewriteRule ^(.*)$ index.php?/invoice/fetch/$1 [L]

which seems to work great! I welcome suggestions/improvements/comments to this however.

更多推荐

本文发布于:2023-08-06 12:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   规则   目录   htaccess   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!