How to Hide Git Folders Using .htaccess

Posted on in: How To's.
Tags:

I needed to hide git folders from the web, but not all the solutions I found on the web seemed to work. Below are the solutions I found that worked without weird side effects like hiding all subfolders. I prefer Solution 2 because the .htaccess file can be under version control. Solution 1 requires you to create the .htaccess file for every environment. Since it’s in the git folder itself it’s not under version control, but this does allow you to enable and disable access on a folder by folder basis. However, both of these methods hide git folders and any subfolders and files within the .git folder.

Band Aid to Hide git Folders

cd .git
vim .htaccess

Press “i” to insert, then copy and paste the following:

Order allow,deny
Deny from all

Better Way To Hide git Folders

cd to the root of your project, then

vim .htaccess

Press “i” to insert, then copy and paste the following:

RedirectMatch 404 "(?:.*)/(?:\.git|file_or_dir)(?:/.*)?$"

Then hit Escape, type “:wq” and hit enter to write to the file and quit vim.

References:
Hide git folder with .htaccess – Bruno de Carvalho on biasedbit
Make .git directory web inaccessible – ThiefMaster on StackOverflow

Logo smleimberg.com