Is it possible to put a directory Directive in an .htaccess file?

The answer to this is no – the Directory directive is not allowed in the .htaccess file. What happens if you try to add a Directory directive to your .htaccess file? Well, if you try to add a Directory directive to your .htaccess file, you will get a “500 Internal Server Error”. This is because Directory directives are simply not supported in .htaccess files.

Where is the Directory directive allowed?

The Directory directive is allowed in the server configuration file – which is usually named httpd.conf – and in any virtual hosts file.

Why is having a Directory section in an .htaccess file not possible?

The purpose of an .htaccess file is to allow web publishers to make changes on their Apache web server on a per directory basis. Because the directives inside an .htaccess file are meant to be applied only to the directory (and subdirectories) in which the .htaccess file lives, using a Directory directive doesn’t always make sense. For example, suppose there’s an .htaccess file inside the /home/username/languages/php directory, and you want to use a Directory directive inside that .htaccess file for /home/username/compilers/cpp. This would not make sense because the .htaccess file is only meant to have it’s directives apply to the /home/username/languages/php directory and it’s subdirectories. Clearly, /home/username/compilers/cpp is out of that directory “scope”, and this is why Apache would output a “500 Internal Server Error” if it saw the use of the Directory directive in an .htaccess file.

A Directory section in main config file is preferable to .htaccess

The Apache documentation itself states that using a Directory section/directive inside the main server config file (usually named “httpd.conf”) is a better alternative to using an .htaccess file. Any and every directive placed inside an .htaccess file can also be made using a Directory directive inside the server configuration file. An.htaccess is meant to be used only when you do not have access to the server configuration file, which is often the case for people who are on shared hosting services. VPS users usually have access to the server configuration file.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

One thought on “Directory directive in .htaccess”