explaingit

phanan/htaccess

13,153Audience · developerComplexity · 1/5Setup · easy

TLDR

A copy-paste collection of Apache .htaccess snippets for URL redirection, security hardening, performance tuning, and common web server configuration tasks, no hunting through separate docs required.

Mindmap

mindmap
  root((repo))
    URL Rewriting
      Force HTTPS
      Remove www
      Clean URLs
      Trailing slash removal
    Security
      Block sensitive files
      Disable directory listing
      Hotlink protection
      Security headers
    Performance
      Gzip compression
      Browser caching
      Cache-Control headers
    Miscellaneous
      Custom error pages
      Force file download
      CORS headers
      PHP version switch
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Force HTTPS and remove or add the www prefix for all visitors to your website.

USE CASE 2

Block directory listing, protect sensitive files, and set HTTP security headers like Content Security Policy.

USE CASE 3

Enable gzip compression and set browser cache expiration headers to speed up your site.

USE CASE 4

Set up custom error pages and allow fonts and assets to load across different domains.

Tech stack

Apache

Getting it running

Difficulty · easy Time to first run · 5min

Requires an Apache web server with .htaccess support enabled, does not apply to Nginx.

In plain English

This repository is a reference collection of .htaccess snippets for Apache web servers. An .htaccess file is a small configuration file you place inside a folder on your web server to control how Apache handles requests to that folder, without touching the main server settings. This repo gathers copy-paste-ready examples so you do not have to hunt them down from separate sources. The snippets are organized into four areas. The first covers URL rewriting and redirection: forcing or removing the "www" prefix, redirecting HTTP traffic to HTTPS, removing trailing slashes, sending visitors from old pages to new ones, and setting up clean URLs that hide file extensions. Each snippet is a short block of Apache configuration syntax with a plain explanation of what it does. The second area covers security. It includes snippets for blocking all visitors except a specific IP address, hiding sensitive files from direct access, disabling directory listing, preventing your images from being loaded by other websites, password-protecting directories or individual files, blocking visitors from particular referrer sites or browser agents, and setting HTTP security headers such as Content Security Policy and Referrer Policy. The third area covers performance: compressing text responses, setting browser cache expiration times, and configuring Cache-Control headers. The fourth area covers miscellaneous tasks: setting custom error pages, forcing file downloads, allowing fonts to load across different domains, enabling CORS, and switching which PHP version Apache uses. The README is written for Apache 2.4. It notes that .htaccess files are slower than editing the main server config, and recommends using them only when you do not have access to that config. You use the snippets by copying the relevant block into your own .htaccess file and adjusting domain names or paths where needed.

Copy-paste prompts

Prompt 1
Give me an .htaccess snippet to redirect all HTTP traffic to HTTPS and remove the www prefix at the same time.
Prompt 2
Write an .htaccess rule to block direct browser access to any .env or .git files on my Apache server.
Prompt 3
Show me the .htaccess snippet to enable gzip compression for HTML, CSS, and JavaScript files.
Prompt 4
Create an .htaccess snippet that sets a 1-year cache expiration for images and a 1-week expiration for CSS and JS files.
Prompt 5
Write an .htaccess rule to password-protect a specific folder on my Apache site.
Open on GitHub → Explain another repo

← phanan on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.