简体   繁体   English

htaccess文件扩展在重写中导致404

[英]Htaccess file extension in rewrite causing 404

I am having a problem with one of my rewrite rules. 我的一个重写规则有问题。 I am in the process of developing an application where I want to control file access via a script. 我正在开发要通过脚本控制文件访问的应用程序。 My plan is to simply pass a rewrite rule through to my script with parameters to retrieve the appropriate file(s). 我的计划是简单地将带有参数的重写规则传递给我的脚本,以检索适当的文件。

However, I am having some issues when trying to pass a file name with an extension as part of a rewrite rule. 但是,尝试将带有扩展名的文件名作为重写规则的一部分时遇到一些问题。 Perhaps someone can point out what i'm doing wrong. 也许有人可以指出我在做什么错。

My rewrite rule: 我的重写规则:

RewriteEngine On
RewriteBase /
RewriteRule ^files/(.+)/(.*)$ /index.php/files/$1/$2 [NC,L]

What works: http://localhost/files/12345/index 什么有效: http://localhost/files/12345/index

What is causing 404 errors to be thrown: http://localhost/files/12345/index.css 是什么导致引发404错误: http://localhost/files/12345/index.css

I've been battling with this for quite some time now, it almost acts like it's trying to load the css file, not finding it, and never running the rerwite rule. 我已经为此进行了一段时间的争斗,它的行为几乎就像是试图加载css文件,没有找到它,并且从不运行rewwite规则。 I am using IntelliJ's PHP Built-in Web Server for my host environment, not sure if that has anything to do with it or not. 我正在为我的主机环境使用IntelliJ的PHP内置Web服务器,不确定是否与此有关。

UPDATE: 更新:

If I go to http://localhost/index.php/files/12345/index.css directly the page works as expected, it does not return a 404. This rule is the only rule in my .htaccess file. 如果我直接转到http://localhost/index.php/files/12345/index.css ,该页面将按预期工作,则不会返回404。此规则是.htaccess文件中的唯一规则。

If I echo the $_SERVER["PATH_INFO"]; 如果我回显$_SERVER["PATH_INFO"]; from the index.php I get /files/12345/index.css back as the value 从index.php我得到/files/12345/index.css作为值

You need to skip existing files and directories from this rewrite: 您需要跳过此重写中的现有文件和目录:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(files/[^/]+/.*)$ /index.php/$1 [NC,L]

Found that the problem was simply that PHP Built-In Web Server doesn't honor .htaccess the same as apache does. 发现问题仅是PHP内置Web服务器不像.apache那样支持.htaccess。 Tossed this to a full blown apache server, and everything worked like a charm. 把它扔到一个功能强大的apache服务器上,一切都像个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM