Forum Moderators: phranque

Message Too Old, No Replies

htaccess affecting whole site

         

apauto

7:52 am on Dec 23, 2008 (gmt 0)

10+ Year Member



How can I have an htaccess file only affect the main site, and not subfolders?

Right now, I have an htaccess file in a subfolder, but the main site's htaccess overwrites it, causing some serious havoc.

Any ideas?

Thanks guys!

phranque

10:14 am on Dec 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



from the apache doc [httpd.apache.org]:
A [.htaccess] file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

what do you mean by "the main site's htaccess overwrites it"?

jdMorgan

3:42 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Overrides it" is likely what was meant.

In the main .htaccess, add exclusions for URL-paths which resolve to to the subdirectory.

-or-

Rearrange the file structure so that interference will no longer occur, using mod_rewrite to map (internally rewrite, not externally redirect) the current URLs to their new file locations.

If the problem is entirely caused by mod_rewrite directives in the root overriding mod_rewrite directives in the subdirectory, then see "RewriteOptions none" as the 'opposite' of "RewriteOptions inherit".

Jim

apauto

4:03 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



In the main .htaccess, add exclusions for URL-paths which resolve to to the subdirectory.

How would I block a subdirectory from the main sites htaccess? I tried searching, but couldn't find anything.

Thanks so much!

jdMorgan

4:28 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using RewriteConds on RewriteRules and <Files> containers. If you have mod_alias Redirects, change them all to use mod_rewrite so you can add exclusions (don't mix mod_alias and mod_rewrite directives, as you cannot control order of execution if you do).

Depending on what directives you are using, it may not be possible to exclude subdirectories using these methods. But since it's easier than re-arranging your filesystem in accordance with access controls, I thought I'd mention it.

Jim

apauto

5:10 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



jd, thank so much for your reply!

What would I do here to have it only affect the base directory, and not the subdirectories?

Thanks so much you're a life saver!

Options +FollowSymLinks
# //seo_mod_start
RewriteEngine On
RewriteBase /

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

RewriteRule ^forums.html$ forums.php [L,NE]
RewriteRule ^(.*)-t-([0-9]+).html(.*)$ showthread.php?tid=$2$3 [QSA,L]
RewriteRule ^(.*)-t-([0-9]+)-([0-9]+).html$ showthread.php?tid=$2&page=$3 [QSA,L]
RewriteRule ^(.*)-f-([0-9]+).html(.*)$ forumdisplay.php?fid=$2$3 [QSA,L]
RewriteRule ^(.*)-f-([0-9]+)-([0-9]+).html(.*)$ forumdisplay.php?fid=$2&page=$3 [QSA,L]
RewriteRule ^(.*)-f-([0-9]+)-([a-z]+)(-¦-[a-z]+)-([0-9]+)-([0-9]+).html(.*)$ forumdisplay.php?fid=$2&sortby=$3&order=$4&datecut=$5&page=$6$7 [L]
RewriteRule ^(.*)-a-([0-9]+).html$ announcements.php?aid=$2 [L]
RewriteRule ^(.*)-([0-9]+).html$ member.php?action=profile&uid=$2 [QSA,L]

jdMorgan

5:25 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you haven't said what subdirectory paths are causing problems or what kind of problems you're having, but if the subdirectroy URL-paths are paths that match your second group of rules, then the simplest method would be to use a more-specific pattern than ".*" -- which matches anything and everything. For example, your third rule:

RewriteRule ^([^/]+)-t-([0-9]+)-([0-9]+)\.html$ showthread.php?tid=$2&page=$3 [QSA,L]

That will accept one or more characters as long as none of those characters is a slash, and therefore, the pattern won't match any subdirectories.

Note also that the literal periods in the URL-path must be escaped as shown.

Best practice: Never use the easy, ambiguous, greedy, and promiscuous pattern ".*" unless it cannot be avoided.

Jim