.htaccess multi-leveling
March 9, 2005 12:56 PM   Subscribe

How do I set up .htaccess files in both a directory and sub-directory so they don't conflict?

Running Apache. Using .htaccess to set separate level permissions and options. Have one in a directory and its sub-directory that both redirect to separate scripts. How do I force it to go to the sub-directory and use its .htaccess instead of redirecting in every case? eg. [mywebsite.com]/.htaccess and [mywebsite.com]/news/.htaccess if someone types [mywebsite.com]/news/ they instead get the script referenced by [mywebsite.com]/.htaccess.
posted by mystyk to Computers & Internet (5 answers total)
 
I'm not entirely sure I understand. The sub-dir inherits the directives, so if you don't want that, you have to set the directives back to their default value. For instance, if you're using mod_rewrite on the parent, and you want to disable it on the sub-dir, you would add this to the sub-dir .htaccess:

RewriteEngine off
posted by smackfu at 1:07 PM on March 9, 2005


Response by poster: Ok, to clarify, I use mod rewrite on both, sending everything to a script (one at each .htaccess's level) that then parses the Request_URI and for the one at the directory level searches through a database. At the sub-directory level, it does it's own thing outside of a database, which works fine if the directory level one is not present. At the directory level, it all works as long as I'm not trying to access the sub-directory, which gets sent as the URI to the directory's script and parsed with no results in the database. Is there some way to redirect it internally to the proper directory before the lower-level script would be called so that it utilizes the proper sub-directory level's .htaccess?
posted by mystyk at 1:17 PM on March 9, 2005


Best answer: Maybe add this to the parent .htaccess before the existing RewriteRule, to have the rewrite rules not apply to the subdir:

RewriteCond %{REQUEST_URI} !^/subdir
posted by smackfu at 1:43 PM on March 9, 2005


Response by poster: Ok, I see where you're going, but what if there are more parts to the URI then just /subdir ? eg. [mysite.com]/news/5 in order to get the script to skip the first 5 news posts, where /news is the subdir (that's what I meant about parsing the URI to its components for other actions. The main folder searches through a database based on the URI, the subdir parses an xml page.)
posted by mystyk at 2:58 PM on March 9, 2005


Response by poster: Ok, for a return on this, I tried multiple forms with no success at either the subdirectory or sub with arguments level.
Here's what I tried:
RewriteCond %{REQUEST_URI} !^/subdir
RewriteCond %{REQUEST_URI} !^/subdir$
RewriteCond %{REQUEST_URI} !^/subdir [NC]
RewriteCond %{REQUEST_URI} !^/subdir$ [NC]
of course, I tried it with a test subdirectory I set up, not "subdir"
I further set it up with an .htaccess in the subdir redirecting to a text file that contained junk data.
Any further ideas?
posted by mystyk at 3:33 PM on March 9, 2005


« Older Ergonomic keyboard with trackpoint mouse?   |   Anyone know of a good public torrent tracker I can... Newer »
This thread is closed to new comments.