Working with Directory Controls
1. Using Directory Directive
| Directive | Values | Description |
|---|---|---|
| Directory | <Directory "/path/to/dir"> |
Defines a configuration block for a specific directory |
| DirectoryMatch | <DirectoryMatch "regex"> |
Applies configuration based on a regex match of the directory path |
| Files | <Files "filename"> |
Applies configuration to specific files |
| FilesMatch | <FilesMatch "regex"> |
Applies configuration to files matching a regex pattern |
| Location | <Location "/path"> |
Applies configuration to a specific URL path |
| LocationMatch | <LocationMatch "regex"> |
Applies configuration to URLs matching a regex pattern |
Example: Basic Directory Configuration
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Example: Using DirectoryMatch
<DirectoryMatch "^/var/www/.*\.html$">
Options -Indexes
AllowOverride None
Require all granted
</DirectoryMatch>
Note: The
Directory directive is processed in order, and later directives can override earlier ones.
Warning: Misconfiguring directory permissions can lead to security vulnerabilities. Always test configurations in a safe environment.
2. Configuring Directory Options
| Option | Description | Default | Notes |
|---|---|---|---|
| Options | Controls which server features are available in a directory | None | Can include multiple options separated by spaces |
| Options Indexes | Enables directory listing if no index file is found | Disabled | Requires AllowOverride Indexes to be set |
| Options FollowSymLinks | Allows following symbolic links to directories | Enabled | Disabled in some security contexts for safety |
| Options SymLinksIfOwnerMatch | Allows symbolic links to directories only if the owner matches | Disabled | More secure alternative to FollowSymLinks |
| Options MultiViews | Enables content negotiation based on file extensions | Disabled | Can cause performance issues in some setups |
| Options All | Enables all options except None |
Disabled | Use with caution as it enables potentially unsafe features |
Example: Enabling Directory Indexing
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Example: Disabling Symbolic Link Following
<Directory "/var/www/secure">
Options -FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Note: The
Options directive can be overridden by AllowOverride settings in the main configuration or virtual host context.
Warning: Enabling
Indexes can expose directory contents to users, which may be a security risk.
3. Setting DirectoryIndex Files
| Directive | Values | Description |
|---|---|---|
| DirectoryIndex | index.html |
Specifies the default file to serve when a directory is requested |
Example: Setting DirectoryIndex
<Directory "/var/www/html">
DirectoryIndex index.html index.php
</Directory>
Note: The server will look for files in the order listed and serve the first one found.
4. Configuring AllowOverride
| Directive | Values | Description |
|---|---|---|
| AllowOverride | None, ALL, AuthConfig, FileInfo, Indexs, Limit, Options |
Controls which directives can be overridden by .htaccess files |
Note: Setting
AllowOverride to ALL allows all directives to be overridden, which can pose security risks in shared hosting environments.
Warning: Misconfiguring
AllowOverride can lead to unexpected behavior or security vulnerabilities. Always test in a development environment first.
5. Using Files Directive
| Directive | Description | Default | Context |
|---|---|---|---|
| Files | Applies configuration to specific files or directories matching a pattern. | None | Server config, virtual host, directory |
Note: The
Files directive is processed before other directives and can be used in combination with FilesMatch for more complex matching.
6. Using Location Directive
| Directive | Description | Usage |
|---|---|---|
| Location | Applies configuration to a specific URL path or resource | <Location "/admin"> |
| LocationMatch | Applies configuration based on a regex pattern matching the URL | <LocationMatch "^/api/.*\.json$"> |
| LocationMatch | Applies configuration based on a regex pattern matching the URL | <LocationMatch "^/api/.*\.json$"> |
Example: Enabling CORS for API Endpoints
<LocationMatch "^/api/.*">
Header set Access-Control-Allow-Origin "*"
</LocationMatch>
Note: The
Location directive is processed after the Directory and Files directives, making it ideal for URL-specific configurations.
7. Using DirectoryMatch for Pattern Matching
| Directive | Description | Usage |
|---|---|---|
| DirectoryMatch | Applies configuration to directories matching a regex pattern | |
| FilesMatch | Applies configuration to files matching a regex pattern | |
Example: Restricting Access to Specific Directory Patterns
<DirectoryMatch "^/var/www/[^/]+/private/">
Require ip 192.168.1.0/24
</DirectoryMatch>
Example: Setting Cache Headers for Assets
<FilesMatch "\.(jpg|jpeg|png|gif|ico)$">
Header set Cache-Control "max-age=31536000, immutable"
</FilesMatch>
Note:
DirectoryMatch uses Perl-compatible regular expressions (PCRE) and is evaluated in the order of configuration files.
8. Configuring FollowSymLinks Option
| Directive | Values | Description |
|---|---|---|
| Options | FollowSymLinks |
Allows the server to follow symbolic links in the filesystem |
| Options | -FollowSymLinks |
Disables following symbolic links (default in some configurations) |
Example: Enabling FollowSymLinks in .htaccess
<Directory "/var/www/html">
Options +FollowSymLinks
</Directory>
Note: Enabling
FollowSymLinks can pose security risks if not properly configured, as it allows traversal through symbolic links to files outside the intended directory structure.
9. Enabling and Disabling Indexes
| Directive | Values | Description |
|---|---|---|
| Options | Indexes |
Enables directory listing for the specified directory |
| Options | -Indexes |
Disables directory listing for the specified directory |
Note: When
Indexes is enabled, Apache will display a directory listing if no index.html or similar file is present.
10. Setting Up Fancy Indexing
Example: Basic Fancy Indexing Setup
<Directory "/var/www/html">
Options +Indexes +FancyIndexing
</Directory>
| Directive | Description | Default | Notes |
|---|---|---|---|
| Indexes | Enables directory listing | Off | |
| FancyIndexing | Enables fancy directory indexing with icons and colors | Off | Requires Indexes to be enabled |
| IconWidth | Width of icons in pixels | 16 | |
| IconHeight | Height of icons in pixels | 16 | |
| HeaderName | File to use for header content | None | |
| FooterName | File to use for footer content | None |
Note: Fancy indexing requires the
mod_autoindex module to be loaded.
Example: Customizing Indexing with Icons
<Directory "/var/www/html">
Options +Indexes +FancyIndexing
IndexOptions +IconsAreLinks +SizeSuppressed
</Directory>
| IndexOption | Description | Default | Notes |
|---|---|---|---|
| IconsAreLinks | Icons link to the file instead of just showing the icon | Off | |
| SizeSuppressed | Suppresses display of file sizes | Off | |
| NameWidth | Width of name column in characters | 30 | |
| TimeFormat | Format for displaying timestamps | %d-%b-%Y %H:%M |
Can be overridden with %d-%m-%Y %H:%M |
| SuppressLastModified | Hide last modified date | Off |
11. Understanding Directive Merge Order
| Directive | Merge Type | Description |
|---|---|---|
| Directory | Override |
Controls how directives in .htaccess files are merged with those in the main server config |
| Files | Override |
Merges directives within the same context, allowing for more granular control |
| Location | Override |
Applies directives to specific URL paths, merging with other Location blocks |
Example: Merging Directory and Location Blocks
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
<Location "/api">
Require all granted
</Location>
Note: The
AllowOverride directive controls whether .htaccess files can override server configuration directives.
Directive Merge Order Summary
- Directory: Merges within directory contexts
- Files: Merges within file contexts
- Location: Merges based on URL path matching
12. Controlling Directory Inheritance
| Directive | Description | Default | Context |
|---|---|---|---|
| AllowOverride | Controls which directives can be overridden in .htaccess files | None | Directory, .htaccess |
| Options | Enables or disables specific server features for a directory | None | Directory, .htaccess |
| DirectoryIndex | Specifies the default file to serve when a directory is requested | index.html | Directory, .htaccess |
Example: Options Directive Usage
<Directory "/var/www/html">
Options Indexes FollowSymLinks
</Directory>
Note: The
AllowOverride directive controls whether directives in .htaccess files can override server configuration. Setting it to All allows all directives to be overridden.
Warning: Setting
AllowOverride to All can introduce security risks if not carefully managed.