Directive /h2>
When working with shared hosting environments where you don't have access to httpd.conf, you need to achieve the same authentication effect using .htaccess. The key is to use
Here's the complete implementation that mirrors the
# Enable basic authentication
AuthType Basic
AuthName "Trac"
AuthUserFile /path/to/your/trac.htpasswd
Require valid-user
# Only apply to login URL
Satisfy Any
Order allow,deny
Allow from all
Require valid-user
# Alternative using mod_rewrite if FilesMatch doesn't work
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/trac/login
RewriteRule .* - [E=require_auth:1]
<IfDefine require_auth>
Require valid-user
</IfDefine>
The first approach uses
Key components:
- AuthType Basic: Specifies basic authentication
- AuthName: Sets the realm for authentication
- AuthUserFile: Points to your password file
- FilesMatch: Regex pattern to match exactly "login"
- mod_rewrite alternative: Uses environment variables to conditionally require auth
After implementing this solution:
- Test by accessing /trac/login - should prompt for credentials
- Access other Trac pages (/trac/wiki, /trac/timeline) - should not require authentication
- Check server error logs if you encounter issues
Remember to:
- Use absolute paths for AuthUserFile
- Generate the .htpasswd file using htpasswd command
- Set proper permissions (644 for .htaccess, 600 for .htpasswd)
When working with shared hosting where httpd.conf modifications aren't possible, we need to translate Apache's <Location> directives into .htaccess-compatible syntax. The equivalent approach uses <FilesMatch> or URL rewriting for path-based restrictions.
For protecting just the Trac login page while leaving other pages accessible:
<FilesMatch "^login$">
AuthType Basic
AuthName "Trac"
AuthUserFile /path/to/trac.htpasswd
Require valid-user
</FilesMatch>
If you need path-based matching (like the original /trac/login URL):
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/trac/login
RewriteRule .* - [E=require_auth:1]
<IfDefine require_auth>
AuthType Basic
AuthName "Trac"
AuthUserFile /path/to/trac.htpasswd
Require valid-user
</IfDefine>
For Trac installations using CGI or WSGI, ensure these additional settings exist in your .htaccess:
SetEnv TRAC_ENV "/path/to/trac/environment"
ScriptAlias /trac "/path/to/trac/cgi-bin/trac.cgi"
After implementing, test with:
curl -I http://yoursite.com/trac/login
You should receive a 401 Unauthorized response before successful authentication.
If authentication isn't triggering:
- Verify the .htaccess file is in your Trac root directory
- Check Apache's AllowOverride settings (must include AuthConfig)
- Confirm the AuthUserFile path is absolute and accessible