Hello, all:

Wandering if anybody can help me with this:

can I have more than one htaccess file in a site? as in having htaccess files in subdirectories to control or specify how files in that directory refer to "include" files? so that if I have a mini-site within a site (as in ), the sports sections will only read includes that are inside the Sports folder and not the root folder?

is that even the proper way to do that??

Thanks for your help!

Dani AI

Generated

Short answer: yes — Apache will read a separate .htaccess in each directory and those files apply to that directory and its children (subject to the server's AllowOverride settings). See the Apache how-to on per-directory .htaccess use (Apache .htaccess docs) — as pointed out, per-directory files are supported.

Important distinction: .htaccess is an Apache feature. Controlling which files PHP scripts can include is done at the PHP level (or by filesystem permissions). Practical ways to make a subsite only use local includes:

  • Prefer application-level control (portable and reliable). Use absolute filesystem paths or __DIR__ so includes always come from the subdirectory, e.g.:

    require_once __DIR__ . '/includes/config.php';

    or adjust the runtime include path from PHP:

    set_include_path(__DIR__ . '/includes' . PATH_SEPARATOR . get_include_path());
    require 'config.php';

    (These work regardless of how PHP is hosted; see PHP include-path docs (include_path).)

  • If you must use .htaccess to enforce a directory-only include path, you can set include_path there — but only when PHP runs as mod_php and your host allows php_value in .htaccess. Example (may cause a 500 error on CGI/FastCGI/PHP-FPM):

    # /sports/.htaccess — works only with mod_php and AllowOverride permitting php_value
    php_value include_path "/home/username/public_html/sports/includes"

    For stronger file-access restrictions, open_basedir is the intended setting, but it often must be set in php.ini or the virtual-host config (or via per-user ini files like .user.ini on some setups). See PHP docs on per-directory configuration and open_basedir (Per-directory INI files, open_basedir).

Troubleshooting tips: check phpinfo() in the subdirectory to confirm include_path/open_basedir, watch Apache/error logs for "Invalid command 'php_value'" errors, and prefer setting these rules in the main vhost config for performance if you control the server (AllowOverride note: https://httpd.apache.org/docs/current/mod/core.html#allowoverride). Security note: combine good include paths with correct file ownership/permissions; include_path alone is not a complete security boundary.

Recommended Answers

All 2 Replies

I'm having a hard time understanding what you're trying to accomplish; however, it is possible to have .htaccess files within folders that control what happens in that folder and it's subdirectories.

Thanks Mvied!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.