Practical tools for system, network, security, and operations administrators.

SYS-C0162 · Web Servers & Hosting

.htaccess and mod_rewrite Explained

Explain one .htaccess rewrite you can read aloud, and do not open a directory listing.

Updated 25 October 2026 · Explainer

This is an original planning guide. It is not a vendor manual, an official standard, or legal advice. Example addresses use documentation ranges. Review every command before you paste it.

What this hour is for

.htaccess is a per-directory Apache file. mod_rewrite changes a URL. The tester reads a rule you paste. The generator drafts a rule. Neither tool edits a server.

One rule, one test URL. Do not enable directory listing to see if the file is there. Do not write a rule that sends people to an address taken from the query string. Do not rewrite away an authentication check.

If the server is Nginx, this file does nothing. The migration page is the later conversion. 192.0.2.10/24 and example.com are documentation. They stay in the note.

The change you can finish

Apply nothing while you are writing the note. When the lab window opens, it is one change on the host named in the ticket. Read the draft, then the host, then decide.

Rollback is the file, the package version, or the snapshot you saved first. You do not invent a rollback after the only copy is gone.

  1. Confirm the server is Apache.
  2. Write the test URL.
  3. Draft or paste one rule.
  4. Reject a listing and an open redirect.
  5. Do not install the file during the reading.
  6. Stop.

Mistakes that outlive the command

.htaccess and mod_rewrite Explained goes wrong in a small set of ways. The list is here so the ticket can forbid them before the window.

Decisions to write down

One decision for .htaccess and mod_rewrite Explained: The server is Apache. One rule. No listing. No open redirect. And the file was not installed yet.

Checks before you call it done

Confirm this before you call the change done: The test URL is written. The rule matches that URL. Listing is off. No redirect to a query-string host. And nothing was installed. If one of those fails, stop.

# One rule.
# No directory listing.
# Do not redirect to a host taken from the query string.

Before you leave the host

.htaccess and mod_rewrite Explained ends when the hostname in the prompt matches the hostname in the ticket. If it does not, you are on the wrong host. Stop. A command that succeeded on the wrong host is not progress.

The draft stays a draft until a person has read the names in it. A generator does not log into the host and does not know which disk is empty. You delete a line that names a host, a path, or an account you did not write down.

192.0.2.10/24, gateway 192.0.2.1, 198.51.100.0/24, 203.0.113.0/24, and 2001:db8::/32 are documentation. example.com is a documentation name. They belong in the note. They are not values you leave on a host because a draft included them.

prompt-for-this is a reminder that a secret is missing. It is not a password, a key, or a token you save in the ticket or in git. The real secret stays in the password store.

One change is the window. If two things move and the result is wrong, you will not know which one did it. The next change waits for its own page.

If the same step fails twice, stop for the day. The failure is the result. Adding a forum command beside a half-applied change produces a host nobody can rebuild. Restore the copy you took first.

A second person reads the hostname and the change aloud. If they cannot find them in the ticket, the note is not done. Silence is not agreement. Then leave the host alone.

This page is a planning guide. It is not a vendor manual, not an official standard, and not permission to touch a host you do not administer. You do not disable a firewall, AppArmor, or SELinux to make a check pass.

Tools for this guide

These tools run in the browser on this site. They do not log into your machines.

More in this cluster

The pillar for this page, and the next published pages filed with it.