我不是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.
更多推荐
发布评论