Matt van Andel
Matt van Andel

Reputation: 657

X-Sendfile displaying 404 for handler file

I'm attempting to serve files through X-Sendfile via some creative .htaccess rewrites. Boiling it down to the bare essentials, I basically catch any incoming request and route it to a handler file, like so...

.htaccess:

# If the requested file exists...
RewriteCond %{REQUEST_FILENAME} -f
# Send the request to handler.php instead
RewriteRule . /handler.php [L]

This is the handler.php:

<?php
header( 'Content-Type: text/html' );
header( "X-My-Debug: {$_SERVER['REQUEST_URI']}" );
header( "X-Sendfile: {$_SERVER['REQUEST_URI']}" );
die();

Now, ideally, if I request an extant file, such as localhost/helloworld.htm, I should see the contents of helloworld.htm in the browser. Instead, I see:

Not Found: The requested URL /handler.php was not found on this server.

Odd, the request is routed to the handler, but that is NOT the filename being sent to X-Sendfile. Also, my custom debug header is missing from the response. So of course, the first thing I do is try the handler with...

<?php
header( 'Content-Type: text/html' );
header( "X-My-Debug: {$_SERVER['REQUEST_URI']}" );
header( "X-Sendfile: helloworld.htm" );
die();

If I do that, then localhost/helloworld.html still gives the above error... however, when I now access localhost/handler.php directly, it finally does serve me the hard-coded helloworld.htm content, and my debug header is now present in the response. The results are the same regardless of whether I replace the hard-coded file with a relative or absolute path.

So what am I missing here? Why is X-Sendfile giving me a 404 error on the HANDLER file when I rewrite a request, but not when I access the handler directly? And why would my custom debug header go missing?

For the record, I'm running this through MAMP 4.1.1.

Upvotes: 0

Views: 646

Answers (1)

that-ben
that-ben

Reputation: 275

X-Sendfile will serve a 404 NOT FOUND error page when the path is correct, but the file does not have the adequate read/write/execute permissions. Try to chmod your file to 0644 and try again to see if it fixes the issue.

Upvotes: 1

Related Questions