0

I have an apache HTTP server with a directory structure as such:

/
---- api/
---- ---- index.php
---- ---- .htaccess
---- index.php
---- .htaccess

/.htaccess:

DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/

/api/.htaccess

RewriteEngine On

My objective was to display the index.php of a directory when it was called without a trailing backslash. However, calling http://domain.com/api results in a 404. Commmenting out the one line in /api/.htaccess causes everything to work as expected.

I'm having a hard time understanding this behavior as the doc for RewriteEngine On says nothing about it. Could someone shed some light on mod_rewrite's workings here?

Note: This question was originally posted on SO here but received no response. I will close it if this one solves the issue.

EDIT: As per @Jonah B's suggestion that a new rewrite context might be created, I tried changing the rules of /api/.htaccess to the following:

RewriteEngine On
RewriteRule ^ index.php

but the result remains the same - it still 404s.

Hele
  • 101
  • 2
  • You might need to add a "RewriteBase /api/" to that configuration, though it should not be necessary if DocumentRoot is set correctly. You can see what apache is doing in the rewrite engine by using trace logging: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging – Jonah Benton Sep 10 '16 at 20:41
  • Increasing logging gives me a bunch of nice information when the page works (i.e. when the second .htaccess is empty), but just this when it 404s: ```[Sat Sep 10 21:13:31.743425 2016] [core:info] [pid 3704:tid 1008] [client a.b.c.d:31361] AH00129: Attempt to serve directory: C:/dir1/dir2/www.domain.com/api```. – Hele Sep 10 '16 at 21:14
  • Then the rewrite engine is not initialized. Try getting rid of the RewriteEngine directive but leaving the RewriteRule directive. – Jonah Benton Sep 10 '16 at 21:23
  • Still the same result =/. It's pretty easy to reproduce, I can send you my host directory if you'd like (Just like 4 files and a directory). – Hele Sep 10 '16 at 21:28
  • Then it sounds like the DirectorySlash Off directive is preventing an internal rewrite to /api/ which would allow it to visit the subdirectory. Turning it on may help. Again, though, it sounds like the desired behavior is available with DirectoryIndex without using the rewrite engine. – Jonah Benton Sep 10 '16 at 21:34
  • That doesn't work either. Using directoryindex doesn't change the behavior at all, but using it with directoryslash turned off sends 301s again, which I don't want. – Hele Sep 10 '16 at 21:46
  • The behavior is reproducible. The problem appears to be that when DirectorySlash is off, and the request lacks a trailing slash, and the main logical to physical mapping resolves it to a subdirectory, and the rewrite engine is engaged, then the results of executing the rewrite rules, in a subrequest, does not wind up being picked up by the main request. This smells like a DirectorySlash bug, but it's not clear, there may be a formulation of RewriteOptions and flags that yields the desired behavior. – Jonah Benton Sep 11 '16 at 00:19

1 Answers1

0

If you only need to tell apache that a request to route

/api

should look for an index.php file in the directory /api maps to, use

DirectoryIndex index.php

Don't bother with the rewrite engine.

See:

http://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex

http://www.htaccess-guide.com/directoryindex-uses/

https://stackoverflow.com/questions/2384423/index-php-not-loading-by-default

Jonah Benton
  • 1,252
  • 7
  • 13
  • You really haven't read the question. – Hele Sep 10 '16 at 18:17
  • If the observation is that explicitly providing a RewriteEngine On directive in a subdirectory .htaccess causes parent directory RewriteRules to be ignored, the implication is that .htaccess is creating a new rewrite context that lacks the rules defined in the parent directory .htaccess. And with DirectorySlash off, no internal redirect to /api/ is occurring. – Jonah Benton Sep 10 '16 at 18:42
  • Now *that* is very interesting. I'll test and get back in a sec. EDIT: Question updated. – Hele Sep 10 '16 at 18:56