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.

  • 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: 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
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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.