I have a website that I use github (closed source) to track changes and update site. The only problem is, it appears the .git directory is accessible via the web. How can I
solution for apache2 (LAMP) server - you have 2 places to add .htaccess contents.. if 1 fails, try next
create .htaccess file in /var/www/html root directory and paste the code inside it
<Directorymatch "^/.*/\.git/">
Order 'deny,allow'
Deny from all
</Directorymatch>
inside virtual host file (/etc/apache2/sites-enabled/) >find your virtualhost file> open file > after closing of virtualhost tag, paste
<Directorymatch "^/.*/\.git/">
Order 'deny,allow'
Deny from all
</Directorymatch>
no need to restart the server, it runs when page is called upon
Instead of messing with .htaccess rules like most answers suggest, why not simply put the .git/ directory above the webroot?
In my setups, my .git directory usually lives in something like:
/home/web/project_name/.git/
My actual code lives in
/home/web/project_name/www_root/
since my web root (as defined on Apache or Nginx.. I prefer the latter) is /home/web/project_name/www_root/ there's no way the .git directory can be accessible from the web since it lives "higher" than the webroot
Both .htaccess and permissions on the .git/ folder would work. I recommend the former:
<Directory .git>
order allow,deny
deny from all
</Directory>
I'm not comfortable with controlling access to my .git folders individually and choose to do it via apache config instead of .htaccess, to prevent me overwriting them, or forgetting on a new install etc.
Here are some detailed instructions hope they help. I'm using Ubuntu 16.10.
A more robust and simple option would be disabling the READ and Execution permission of the .git directory.
Since mostly Apache (httpd) runs under a special user account, for example, it runs as user apache on CentOS, while the .git directory must be created under a real user account, so we can simply block the access by changing the permission. Moreover, this approach doesn't introduce any new file, nor affect the git commands.
The command can be:
chmod -R o-rx .git
Create a .htaccess file in the .git folder and put the following in this file:
Order allow,deny
Deny from all
But note, that it would be lost if you ever re-cloned the repository