Forum Moderators: phranque

Message Too Old, No Replies

rewrites don't work in subdirs with htaccess files

         

hans121

1:14 pm on Apr 24, 2012 (gmt 0)

10+ Year Member



I've got a htaccess file in my root directory (on shared hosting account) with some rewrite rules.
They work on every file as long as there is no htaccess file with other rewrites in the subdirectory containing the file.

not even simple

RewriteCond %{HTTP_HOST} !^origin\.mysite\.com
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]


works for subdirs with htaccess files containing any rewrites.

I've had this problem on two different hosts, what could I be doing wrong?

Some more questions:
If I request a file in a subdirectory, which htaccess file is processed first, the one in the directory I request the file from, or the root htaccess file?

Does RewriteOptions Inherit have to be set for subdirs to honor rules from higher directories?

Anymore info required?

Thanks in advance!

wilderness

2:38 pm on Apr 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



inherit [httpd.apache.org]
This forces the current configuration to inherit the configuration of the parent. In per-virtual-server context, this means that the maps, conditions and rules of the main server are inherited. In per-directory context this means that conditions and rules of the parent directory's .htaccess configuration are inherited.

Rules inherited from the parent scope are applied after rules specified in the child scope.
end of quote

The child takes precedence.
Running the parent htacess on top of the child htaccess could cause a variety of issues, and especially if there were lines in conflict.

hans121

4:01 pm on Apr 24, 2012 (gmt 0)

10+ Year Member



Thanks for answering!

Yep, I've read that already. I mean normally rules specified in the root directory should also apply to all subdirectories, right? Is this because of enabled inheritance or is that a different matter?

wilderness

5:12 pm on Apr 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep, I've read that already. I mean normally rules specified in the root directory should also apply to all subdirectories, right?


ONLY if those sub-directories your referring to are void of their own htaccess.

inheritance is different matter.

hans121

5:51 pm on Apr 24, 2012 (gmt 0)

10+ Year Member



Oh, I wasn't aware of that.
So, for example I have the following situation:

I have a non-www -> www redirect in my root htaccess which should work on all of my pages in all subdirectories. But I have a subdirectory which has it's own htaccess with some rules. Do I have to put the www redirect in this htaccess as well? And all other rules from the root will not apply to this directory?

If that is the case, why then does apache have to check every directory for htaccess files at every request, if they don't really influence each other?

I think I'm still a bit confused...

Is it good to use different htaccess files for every directory or is it generally better to use one big htaccess file at root? Wouldn't that mean every request (even for files in subdirectories) would have to be matched against ALL the rules?

Thank you for helping me out!

g1smd

6:20 pm on Apr 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're almost always better off having all of your rules in a single file in the root.

Apache checks every folder (matching the current request) starting at the root and working downwards.

hans121

6:43 pm on Apr 24, 2012 (gmt 0)

10+ Year Member



@g1smd:
Thanks for the reply.
I just don't get it ;-)
What's the purpose here? Why does it match the request at root and then match again where the file resides, if it doesn't actually apply the rules from root htaccess?

wouldn't having multiple htaccess files for sub-directories mean that requests at root level would have to go through less rules thus are processed faster?

g1smd

7:09 pm on Apr 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you have an internal rewrite in the root and an external redirect in a folder, the internal filepath with be exposed as a new URL when the redirect happens. You have to list the redirect before the rewrite. This and many other situations can arise.

hans121

7:52 pm on Apr 24, 2012 (gmt 0)

10+ Year Member



okay I understand that, thanks. But I think I didn't express myself the right way...

wilderness said that: Rules from the root are applied to subdirectories ONLY if those subdirectories are void of their own htaccess.
Does that apply to any kind of rules?

I have had cases like the following where I would have expected a different behaviour:

this is in the root htaccess:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


and a rewrite in a subdirectory htaccess like this:

RewriteRule ^folder/([^/]+)\.html$ /folder/index.php?cat=$1 [L]



When I request example.com/folder/test.html should I get redirected to www.example.com/folder/test.html first and then the rewrite would fetch content from www.example.com/folder/index.php?cat=test ? Would the external redirect even be applied in this case?

On my server, this doesn't work. I have to have the external redirect in the folder's htaccess as well.
This is what's confusing me. If it's true that apache does not process redirects or rewrites from the root htaccess if I request a file in a subdir (which has it's own htaccess file), why do you say:
Apache checks every folder (matching the current request) starting at the root and working downwards.
- if that's the case, why doesn't it redirect in this case?

Maybe I'm misunderstanding something here... :S

lucy24

12:18 am on Apr 25, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rules from the root are applied to subdirectories ONLY if those subdirectories are void of their own htaccess.

Awk. Someone, somewhere, is interpreting that to mean that if you have

/directory with htaccess
/directory/subdirectory1 with htaccess
/directory/subdirectory2 without htaccess

then requests for material in /subdirectory1 will ignore everything in the higher-level /directory/ htaccess. And I'm morally certain that's not what you meant.

Simplest case: the htaccess file that people will have even if they are deathly afraid of htaccess.

config file (which you can't touch) says "Options +Indexes"
>> all directories everywhere on the server use auto-indexing

/directory/ htaccess says "Options -Indexes"
>> everything in /directory/ and its descendants will have auto-indexing off

/directory/subdirectory1 htaccess says "Options +Indexes"
/directory/subdirectory2 htaccess says nothing about Indexes
/directory/subdirectory3 has no htaccess

>> /subdirectory1 and its descendants will have auto-indexing on. /subdirectories 2 and 3 will continue to have it off. And you can keep toggling this setting, level by level.

Basic rule: Anything said explicitly in a higher-level directory will apply to all descendants of that directory-- with a few exceptions.

One exception that you've probably met: If an htaccess file uses mod_rewrite, it has to contain the line "RewriteEngine On". It doesn't simply get turned on and then stay on for all subdirectories.

If you're on shared hosting, not all directives can go in all places. For example: I have two domains sharing a userspace. I can have a lot of stuff in a shared htaccess, including core "Deny From..." directives. But I can't put the "Options -Indexes" directive up there. It won't make the site crash; it will simply be ignored. (I know this by direct experiment. It is probably not universally true of all shared-hosting setups.)

hans121

10:03 am on Apr 25, 2012 (gmt 0)

10+ Year Member



@lucy24: wow, thanks for the long explanation!

I've tested what you said on my shared hosting account and you are absolutely right. For example, Options -Indexes does work on all subdirectories (with or without htaccess)

However, redirects in root htaccess dont't work for subdirectoriess (with htaccess) even if I set RewriteEngine On (in subdir htaccess as well).
It's been like this on my last host (shared) and the one I'm using now (shared). I'm not using addon domains.
Probably it's just the setup my host is using... hmmm.

thanks