简体   繁体   中英

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

What is causing 404 errors to be thrown: 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. 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.

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.

If I echo the $_SERVER["PATH_INFO"]; from the index.php I get /files/12345/index.css back as the value

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. Tossed this to a full blown apache server, and everything worked like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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