.htaccess Generator
Build a ready .htaccess file: www redirect, forced HTTPS, blocked .git and .env, custom 404 page, compression. Copy it or download the file.
Why an .htaccess generator is worth using
An .htaccess file is a plain text configuration for Apache that changes how a site behaves without access to the control panel and without touching the main server configuration. Most often it holds a redirect from www to the bare domain, a forced HTTPS rule, the move of old pages and a block on service files such as .git and .env. Writing such a file from scratch carries risk: one stray brace or a module the server does not have brings the whole site down with a 500 error. This generator assembles the file from separate rules, each one shipped with a comment, and shows the result as it will land on the server.
What the .htaccess generator does
- Redirects from www to the bare domain and back, built around the domain you type in
- Forced HTTPS and a page move with a choice of the 301 or 302 response code
- Blocks .git, .env and every file whose name starts with a dot
- Disables directory listings, sets UTF-8 as the default charset and points 404 to your own page
- Keeps a folder out of search engines with an X-Robots-Tag header
- Compresses responses, blocks access by IP address and by chosen HTTP methods
How to assemble the file
- Tick the rules you need — each one is added to the file in finished form
- Fill in the domain, the old and new paths, the response code and the path of your 404 page
- Read the finished file: every rule is separated by a comment explaining it
- Copy the text or download the file and place it in the site root
What the server does with the rules
Apache reads an .htaccess file while handling a request and applies it to the directory the file sits in and to every directory below. A file in the site root therefore governs the whole site, while the same file inside an uploads folder only governs that folder. The Require all denied and Require not ip directives arrived with Apache 2.4 and replaced the older Order deny,allow paired with Deny from all. Modules such as mod_deflate, mod_headers and mod_rewrite are enabled separately, which is why the generated file wraps their rules in IfModule: without the module, the block is simply skipped.
Ready rules and what each one does
| What it does | Directive | Requirements |
|---|---|---|
| Redirect www to the bare domain | RewriteCond and RewriteRule with R=301 | mod_rewrite |
| Force HTTPS | RewriteCond %{HTTPS} !=on and RewriteRule | mod_rewrite, a certificate |
| Move an old page | RewriteRule with a 301 or 302 code | mod_rewrite |
| Block .git and .env | FilesMatch and Require all denied | Apache 2.4, AllowOverride FileInfo |
| Disable directory listings | Options -Indexes | AllowOverride Options |
| Default charset | AddDefaultCharset UTF-8 | AllowOverride FileInfo |
| Keep a folder out of search results | DirectoryMatch and Header set X-Robots-Tag | mod_headers |
| Your own 404 page | ErrorDocument 404 /404.html | AllowOverride FileInfo |
| Compress responses | AddOutputFilterByType DEFLATE | mod_deflate |
| Block by IP and by method | RequireAll and LimitExcept | Apache 2.4, AllowOverride Limit |
The directives Require not ip and Require all denied exist in Apache 2.4 and later only.
Where .htaccess earns its place every day
The first scenario is a change of address. After a move to another domain the old links from search engines, from bookmarks and from articles stop working, and every one of them returns a 404. A RewriteRule with the 301 code sends the visitor to the new address and at the same time tells the search engine that the old address is no longer current. The second scenario is making addresses uniform: while a site opens under both example.com and www.example.com, a search engine sees two addresses for one document and splits the weight between them. A redirect towards one canonical form removes the problem.
The third scenario is technical and the most common around payments and internal services: keeping what should never reach the internet out of reach. A .git directory holding the full history of a repository, an .env file with database and mail passwords, database dumps and logs — none of that belongs in front of visitors, and one rule in the site root closes off such files at once. The fourth is a custom 404 page instead of the standard one: it keeps a visitor on the site and offers a link home. The same set of rules also serves during a move to a new CMS, when the old pages answer with a temporary placeholder.
- A domain change and redirects from the old addresses
- Reducing www and non-www to a single canonical address
- Forced HTTPS without editing the main server configuration
- Keeping .git, .env, dumps and logs away from strangers
- Switching off file listings in directories
- A custom 404 page and a folder kept out of search results
Where .htaccess files go wrong
The first mistake is losing a curly brace in RequireAll, FilesMatch or LimitExcept blocks. The closing brace sits on its own line and must end with a slash, otherwise Apache cannot tell where the block ends. The second mistake is writing two rules that contradict each other: a redirect from www and a redirect to www at the same time send the browser into a redirect loop and it reports an endless redirect. The third is forgetting that the Require directives only arrived with Apache 2.4, so the same idea on an older server is written differently.
Two further mistakes come from wrong expectations. The first is expecting an .htaccess file to protect a file stored higher up the tree: rules only apply downwards, so a file inside an uploads folder cannot block something in the site root. The second is assuming that an added X-Robots-Tag header keeps a folder out of search results while mod_headers is not loaded: the block is skipped silently and the folder stays open. Checking what ended up in the file is easier with a regular expression tester, and comparing two versions of a config is what the text comparison is for.
- A missing trailing slash on the closing brace of a block
- Redirects in both directions switched on at once
- Require all denied syntax on Apache 2.2
- Trying to protect a file above the directory holding the .htaccess
- Counting on mod_headers when the module is not loaded
- Forgetting to clear the browser cache when testing a redirect
A worked example: moving an old page
Take a concrete task: the address /old-page no longer works and the page has moved to /new-page. The rule is one line, a RewriteRule with a pattern, a substitution and flags. The pattern is compared against the request path without its leading slash, so it reads old-page, and the question mark at the end makes a trailing slash optional. The substitution is written the other way round, with a leading slash, because it is a finished address.
The R flag sets the response code and L stops processing: once the rule has fired, Apache does not look at the remaining lines of the file. Choose the code deliberately, with 301 for a permanent move and 302 for a temporary one. A pattern that starts with a caret is treated as a regular expression, and that is where escaping of dots starts to matter. The finished example is broken down in the table below, and the file itself can be taken from the code block and uploaded, provided the module check comes first.
What each part of the rule does
| Part | Meaning | Why it is there |
|---|---|---|
| RewriteEngine On | Switches the module on in this file | Without it the rules do nothing |
| RewriteCond | A condition, for example on HTTP_HOST | Narrows a rule down to a host or a protocol |
| ^old-page/?$ | The path pattern without a leading slash | Matches /old-page and /old-page/ |
| /new-page | Where the visitor is sent | Written with a leading slash |
| R=301 | The response code | 301 for good, 302 for temporary |
| L | Stop processing the file | Otherwise a later rule may override the redirect |
| RewriteEngine On once | The directive is not repeated | One occurrence is enough per file |
| Test with a request | The page answers 301 with a Location header | The log shows which rule fired |
After uploading, the browser may still show a cached response, so a redirect is best tested in a private window.
Apache, nginx and neighbouring tools
Before filling the file with rules, it is worth finding out which web server the hosting runs. Apache and LiteSpeed read .htaccess, nginx does not. Under nginx the same tasks are solved with the return, try_files and location directives in the server configuration, and without access to it the rules have to be handed over to the host. A separate check concerns the permissions of the file itself: a very common reason for the file being uploaded while nothing happens, and the permission calculator helps work out the mode.
When part of the job concerns the response code of the server rather than the configuration, the HTTP status code reference shows how a 301 differs from a 302 and what the server answers on an error. Blocking a folder from indexing in .htaccess duplicates a job that can be done at page level, where the noindex meta tag is put together by the meta tag generator. Breaking down the address that redirects point to is easier with a look at a URL broken into parts.
- Working out the permissions of the configuration file
- A reference of the codes a server can return
- Why a particular HTTP error code appears
- Assembling noindex meta tags for individual pages
- Taking a page address apart
- Testing the regular expressions used in the rules
Questions people ask about .htaccess
Where does the .htaccess file belong and what should it be called
Why does the site return a 500 error after the file is uploaded
Does .htaccess work on nginx
What is the difference between a 301 and a 302 redirect
How do I switch off directory listings
How do I block .git and .env
What does AddDefaultCharset UTF-8 actually do
Why are some rules wrapped in IfModule
Related articles
Related tools
htpasswd Generator
An .htpasswd line with a browser-computed SHA-1 hash plus a ready .htaccess block for Apache 2.4 with Require valid-user
Robots.txt Generator
Build a robots.txt for your site: crawl rules, blocked service folders, Clean-param and Host for Yandex, and a Sitemap line
Sitemap.xml Generator
Build a valid sitemap.xml from a URL list or a CSV: lastmod, changefreq, priority, limit checks and a downloadable file
Favicon Generator
Make a site icon from a monogram or your own picture: favicon.ico, apple-touch-icon, Android icons, site.webmanifest and ready HTML tags
JSON Schema Generator
Build a draft JSON Schema from a JSON sample: field types, required keys, nested objects and arrays
Regex Generator
Build a regular expression from a task template or break your own pattern down into parts
Open Graph Generator
Build og:title, og:description, og:image and Twitter Card tags for the link preview on social platforms
ASCII Table
Complete ASCII reference: codes 0–127 in DEC, HEX, OCT and BIN with search by character or code