Changing the .htaccess filename in Apache

Posted in Apache -

Apache allows the use of .htaccess files which can change certain configuration options in a per-directory basis. By default .htaccess files are named .htaccess but it is possible to use a different name either for the whole server, or on a virtual host by virtual host basis. This post looks at how to change the filename used.

Please note that the use of .htaccess files can be switched off and may not be available on your web host's platform. When enabled, the default .htaccess name is usually used but it may be different for your host. It always pays to check your web host's documentation to see what is and is not available.

I am a website developer and use .htaccess files to set up rewrite rules, permanent redirects, specify particular PHP settings and so on. Sometimes the settings in these files need to be slightly different on my development machine from what is set on the production server. In these instances, it is useful to have two different copies of the .htaccess file: one for each server. That way there's no chance of accidentally overwriting the production server's copy with a local copy, because the names are different.

To use a different name from the the default, use the AccessFileName directive in the virtual host's <virtualhost> settings. For example, where the setting is in bold:

<virtualhost>
    ServerName www.example.local
    DirectoryRoot /var/www/www.example.local
    AccessFileName .htaccess.testing
</virtualhost>

The above virtual host would now use .htaccess.testing as its .htaccess file, if it exists. All you need to do is to reload Apache and the new setting will take effect.



Related posts:


Comments