3

I need an .htaccess file that will work whether it is put in the root folder or a subfolder without modification. The script below is the normal one that I've been trying to adapt without success. I tried the solution on htaccess rewrite index.php on root and subfolders and couldn't get it to work.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Layout

.htaccess
index.php
subfolder1
- .htaccess
- index.php

The route /blah should go to /index.php and /subfolder1/whatever should go to /subfolder1/index.php. Currently, the above script will send /subfolder1/whatever to /index.php.

[Update]

This should also work for any path under subfolder1, like /subfolder1/1/2/3/idunno.

Community
  • 1
  • 1
skeemer
  • 323
  • 1
  • 3
  • 11

1 Answers1

1

If you are using Apache 2.2.16 and later, you can just stop using mod_rewrite, which although extremely useful and powerful, can get messy as hell.

A new directive in mod_dir was introduced, FallbackResource which does just that, redirecting to the uri of your choice if there is no hit on the file system. It is available in .htaccess files as long as AllowOverride Indexes is specified for the directories in the configuration.

As .htaccess files are evaluated depth-first, you just have to have each .htaccess file describe your fallback resource in the current directory, and the one in the subdirectory subfolder1 will take precedence:

subfolder1/.htaccess:

FallbackResource index.php

.htaccess:

FallbackResource index.php

They're both the same, and work just right.

It seems this directive is not well known yet even though it's been around for a few years, and its goal is precisely to solve that problem in an elegant way.

There is only one limitation with that setup. Calling urls in non-existing sub-directories of the root dir or subfolder1 will yield subrequest recursion and subsequently an error 500, because the fallback resource is local to the given directory.
The best approach is to have absolute uris (beginning with '/') as parameter to FallbackResource, which is why it is true that the requirement in itself is kind of odd, and is probably not playing too well with the inner workings of Apache.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • Great solution, but won't work for me since I'm looking for something that can be used on shared hosting environments where I can't add mod_dir. – skeemer Sep 24 '13 at 17:15
  • Why is this requirement so odd? I want to develop an app with a router on my machine and it resides in localhost/~username/appname. When I push it into a git repo and pull it onto a server, I don't want to have to modify the .htaccess whether it sits in the root folder or a subfolder. – skeemer Sep 24 '13 at 17:32
  • Maybe odd is not the best word to describe this, but at least it can be said that Apache has trouble working in "relative" ways, especially since parsing .htaccess files is one of the most time-consuming operation in a request handled by this software. Simply put, every rewrite operation taking place in a .htaccess will cause a subrequest round-trip in apache. Not having access to the main apache configuration has always been treated as a "thing to avoid if possible" by apache. – SirDarius Sep 25 '13 at 07:41