Tuesday, August 30, 2022

[SOLVED] How to add .htaccess rules inside <VirtualHost> or inside the httpd.conf file

Issue

A short explanation of what I'm doing is: I need to automatically create virtualhosts for each ip address on my machine, make it point to the vsftpd user directory (/home/xxx) and deny any kind of scripts from being executed.

I want to stop any kind of webpages and especially PHP scripts from being executed, because it would post a huge security risk(apache is sudo). The purpose of this virtualhost is purely to serve game resource files, extentions like .wav , .mdl , .tga , .spr and so on.

I searched around and found this

deny from all 
<filesmatch "\.(avi¦wmv¦mpg¦mov)$"> 
Allow from all 
</filesmatch> 

But this is .htaccess content. How can I implement this functionality of only allowing certain extentions inside my httpd.conf file? It would be a pain to make it use .htaccess, and a risk because users might edit them.

Please refrain from any comments unrelated to my question, such as "sudo apache? you're a dumbass" and so on.


Solution

There is no such thing as .htaccess only content. The is a huge misconception. Most of time you do NOT want to use .htaccess and Apache recommends that you not use it unless necessary. Apache rules can always be put in the server config.

When not to use .htaccess

Now you can put that in your VirtualHost directive. The same location where your document root is defined.

The FilesMatch directive can be used in these context.

Context: server config, virtual host, directory, .htaccess

http://httpd.apache.org/docs/current/mod/core.html#filesmatch

So in your vhost file you can add a Directory directive like this example.

<Directory /path/to/documentroot/>
   Deny from all 
  <FilesMatch "\.(avi|wmv|mpg|mov)$"> 
   Allow from all 
  </FilesMatch> 
</Directory>

If you are using Apache 2.4 then you need to use Require.

<Directory /path/to/documentroot/>
   Require all denied
  <FilesMatch "\.(avi|wmv|mpg|mov)$"> 
   Require all granted 
  </FilesMatch> 
</Directory>


Answered By - Panama Jack
Answer Checked By - Senaida (WPSolving Volunteer)