Joe Meyer
Joe Meyer

Reputation: 4405

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

Upvotes: 0

Views: 352

Answers (2)

Joe Meyer
Joe Meyer

Reputation: 4405

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.

Upvotes: 0

anubhava
anubhava

Reputation: 785661

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]

Upvotes: 0

Related Questions