MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Secure .git repo repository files and directory web inaccessible

Many times, people use git repository to host website in productions by clone-push-pull but it has one drawbacks, it appears the .git directory is accessible via the web. How we can prevent this? Here there are 2 ways which are recommended given below;

  • One redirects to a 404 aka to issue a 404 (w/ mod_rewrite):
  • Redirect it to the domain root

Code Verified in June 2026

To be done in .htaccess in the website main directory


# Safely block all access to .git and related files
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule (^|/).git(/|$) - [F,L]
  RewriteRule (^|/).gitignore$ - [F,L]
  RewriteRule (^|/).gitmodules$ - [F,L]
</IfModule>Code language: HTML, XML (xml)

Summary Table

OptionSecuritySite works?SEO SafeRecommended?
Block Only (.git etc.)โœ… Strongโœ… Yesโœ… Yesโœ… Yes
Redirect EverythingโŒ BadโŒ NoโŒ NoโŒ No

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*/)?.git+ - [R=404,L]
</IfModule>
# Second line of defense (if no mod_rewrite)
RedirectMatch 404 ^(.*/)?.git+


# Make .git files and directory web inaccessible
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*/)?.git+ - [R=404,L]
# Redirect all traffic to the home page
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R=301,L]
</IfModule>

# Second line of defense (if no mod_rewrite)
RedirectMatch 404 ^(.*/)?.git+

# Redirect all traffic to the home page (if no mod_rewrite)
RedirectMatch 301 ^(.*)$ /
Code language: PHP (php)

How to download .git repo from public website?

$ wget --mirror -I .git https://www.domain.com/.git/ --no-check-certificate
$ wget --mirror -I .git https://www.domain.com/.git/Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Apache: Comprehensive List of .htaccess Directives, Rules, and Commands

However, an โ€œabsolutely completeโ€ list is technically impossible to print here in fullโ€”because the .htaccess file can use nearly any Apache directive that is allowed by the…

Read More

Laravel Tutorials: How to run Laravel Projects in Subdirectory

The issue you’re experiencing is common with Laravel applications when deployed in a subdirectory. By default, Laravel’s entry point is the public directory, which means you need…

Read More

Apache non www to www config Setting

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Security practices for using git repository in production for deployment

Many times, people use the git repository to host websites in productions by clone-push-pull but it has one drawback, it appears the .git directory is accessible via…

Read More

Apaceh HTACCESS Tutorial

Introduction What Can I Do? Creating A .htaccess File What is .htaccess Why Not to use .htaccess Error Documents | Custom Error Pages Blocking users by IP…

Read More

Newrelic Tutorials: How to Integrate Apache2 in ubuntu

This tutorial shows how to monitor Apache HTTP Server (apache2) on Ubuntu with the New Relic Apache integration. It covers installing Apache, exposing the mod_status page that…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
RustyNox
RustyNox
3 years ago

But why not deploy .git directory at all, then you can also skip ssh auth on production nightmare, etc.
main.yml

name: Deploy Source Files

on:
 push:
  branches:
   - main

jobs:
 deploy:
  runs-on: ubuntu-latest

  steps:
   - name: Checkout Repository
    uses: actions/checkout@v2

   - name: Copy Source Files
    run: |
     rsync -av --exclude='.git' --exclude='.github' ${{ github.workspace }}/ /path/to/production/server/

1
0
Would love your thoughts, please comment.x
()
x