.htaccess Generator

Build Apache rules for https, www, caching and compression.

  • SEO Tools
  • Free, no sign-up
  • Runs in your browser

.htaccess Generator tool

Rules to include

A syntax error in .htaccess returns a 500 error for the whole directory rather than being ignored. Keep a copy of your working file before replacing it.

About the .htaccess Generator

On shared hosting, the .htaccess file is often the only way to configure the Apache server behind your site. This htaccess generator assembles the rules most sites need, each as a block you can switch on or off with a comment explaining it: forcing HTTPS, choosing between www and non-www, browser caching, gzip compression, custom error pages and basic security headers. A mistake in .htaccess can take a site offline, so keep a backup of the existing file and test changes straight after uploading. For page-by-page redirects, use the 301 Redirect Generator, and for crawler rules, the Robots.txt Generator.

The usual building blocks for shared hosting are all included: redirecting every visit to https, settling on www or no www as the main hostname, long cache lifetimes for images, scripts and stylesheets, gzip or Brotli compression, custom error pages, turning off directory listings, shielding sensitive files and adding basic security headers.

One warning deserves real attention. A typing mistake in .htaccess does not fail gently; Apache answers every request in that folder, and in all the folders below it, with a 500 error. Save the current version somewhere safe before uploading a new one, and rehearse on a staging copy when possible. If you can edit the main server configuration instead, put the rules there, because .htaccess is read again on every single request and slows things down slightly. Keep a copy of the working file before every change, so a single typo can be undone in seconds if the site goes down.

How to use the .htaccess Generator

  1. Choose the rulesEvery block is independent and comes with an explanation.
  2. Enter your domainIt is used in the https and www redirect rules, so type it exactly as visitors should see it.
  3. Read the fileEach block has a comment describing what it does.
  4. Back up, upload and testKeep the old file, because one syntax error takes the whole folder offline.

.htaccess Generator features

  • Permanent redirect from http to https
  • Preferred hostname with or without www
  • Browser caching with sensible lifetimes for each file type
  • Gzip and Brotli compression
  • Custom pages for 404 and 500 errors
  • Folder browsing switched off and private files shielded
  • Starter set of protective response headers
  • Annotated rules you can adjust later

Common uses

  • Moving every visitor onto https
  • Fixing a site that answers on both www and the bare domain
  • Adding cache headers so returning visitors load pages faster without downloading the same files again
  • Switching on compression on shared hosting plans where the main server settings are locked
  • Sending missing pages to a friendly custom 404 page

.htaccess in practice: a shared hosting setup, one-hop https redirects, protecting sensitive files, LiteSpeed servers and WordPress rules

By the FreeToolsWorld editorial team · Updated November 2026 · How we write and check our guides

Many websites in Pakistan run on shared hosting with Apache or LiteSpeed servers, where the .htaccess file is the main way to control redirects, caching and security without access to the server's configuration. This guide builds a practical file for a small business site and covers one-hop redirects, protecting sensitive files, directory listings, image hotlinking, LiteSpeed servers and living alongside WordPress rules.

A worked example: a shared hosting site

A small business site on cPanel hosting needs https, a single preferred domain without www, a custom error page and basic protection. Its .htaccess file includes:

  • RewriteEngine On
  • RewriteCond %{HTTPS} off [OR]
  • RewriteCond %{HTTP_HOST} ^www\. [NC]
  • RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
  • ErrorDocument 404 /404.html

Any request using http or www is sent straight to the https, non-www address in one step, and missing pages show the site's own helpful error page.

One hop to the preferred address

Combining the https and www conditions into one rule, as above, avoids a chain in which visitors are first sent to https and then to the non-www address. Fewer hops mean faster loading and cleaner signals for search engines.

Protecting sensitive files

Configuration files such as .env, backups and logs must never be downloadable. A block such as a FilesMatch section matching names starting with .env, containing Require all denied, refuses such requests. Better still, keep sensitive files outside the public folder altogether.

Directory listings

If a folder has no index file, some servers list its contents, exposing file names to anyone. Adding Options -Indexes switches listings off, so visitors see an error instead of a list of uploaded files.

Image hotlinking

Other sites sometimes embed your images directly, using your bandwidth. Rules that check the referring address can block or replace images requested from other domains. Allow your own domains and major search engines, so images still appear in image search and social previews.

LiteSpeed servers

Many shared hosts use LiteSpeed rather than Apache. LiteSpeed reads .htaccess files and supports most Apache rewrite rules, so the same rules generally work. Hosts may also provide caching plugins that add their own .htaccess sections; leave those sections intact.

Living with WordPress rules

WordPress writes its own rules between # BEGIN WordPress and # END WordPress markers and may rewrite that section when settings change. Place custom rules above the WordPress block so they run first and are not overwritten.

Editing safely

A typing mistake in .htaccess can take the whole site down with a server error. Download a copy before editing, change one thing at a time and reload the site after each change. If an error appears, restoring the copy brings the site back immediately.

Common mistakes

  • Creating two-step https and www redirects.
  • Leaving .env files and backups downloadable.
  • Editing without a backup copy.
  • Placing custom rules inside the WordPress block.

Related tools

To build redirect rules for changed pages, use 301 Redirect Generator. To add protective headers, try Security Headers Generator, and to guide crawlers, Robots.txt Generator.

Compression

Rules using mod_deflate compress text files, such as HTML, CSS and JavaScript, before sending them, making pages load faster. Many hosts enable compression already, so check the response headers for content-encoding before adding rules, to avoid duplicating what the server does.

Blocking abusive addresses

Apache 2.4 can refuse requests from specific IP addresses or ranges with Require not ip lines inside a RequireAll block. Use this sparingly for clearly abusive sources, keep a note of why each address was blocked and review the list occasionally, since addresses change hands.

PHP settings on modern hosts

Lines such as php_value in .htaccess work only when PHP runs as an Apache module. Many hosts now run PHP separately, through PHP-FPM, where such lines cause server errors. On those hosts, PHP settings belong in a .user.ini file or the hosting panel's PHP settings instead.

Password protection

Folders such as a staging copy or an internal reports area can be protected with basic authentication rules in .htaccess, pointing to a password file stored outside the public folder. Always combine this with https so passwords are never sent unencrypted.

Checking what went wrong

When a rule misbehaves, the hosting panel's error log usually names the line and the problem. Combined with checking responses with curl or the browser's network panel, it turns guesswork into a quick fix.

Keep it tidy

Over the years, .htaccess files collect rules from old plugins, past migrations and abandoned experiments. Reviewing the file occasionally and removing rules that no longer apply keeps it understandable and avoids unexpected interactions between old and new rules.

Rules for subfolders

An .htaccess file applies to its own folder and everything below it, and files in subfolders can override parent rules. When a rule seems to be ignored in one section of a site, check that folder for its own .htaccess file, which may contain conflicting settings.

Privacy

Rules are generated in your browser. Nothing is sent anywhere or stored.

Frequently asked questions

What happens if the file contains a mistake?

Apache responds with a 500 error for that folder and everything inside it, so the site goes down instead of skipping the bad line. Download the current file first so you can restore it within seconds.

Is .htaccess better than the main server configuration?

Server-level configuration wins on speed, since Apache parses .htaccess afresh for each request. Use .htaccess when you cannot change the server itself, which is the normal situation on shared hosting.

Should my site use www or not?

Neither choice is better for search engines. The important thing is to commit to one form and forward the other, so reports and rankings are not split across duplicate hostnames.

Will these rules work on an nginx server?

No. The syntax belongs to Apache, and nginx ignores .htaccess files completely. Use the nginx output of the Redirect Generator and add caching and compression settings to your nginx server block instead.

How long should images and scripts be cached?

Twelve months suits fingerprinted files, because any update gives them a new name. Files whose names never change should get a shorter lifetime so visitors do not keep an outdated copy.

Will these settings make my website faster?

Caching and compression help, often a lot on a site sending uncompressed files. They do not replace the bigger win, which is sending less in the first place, such as smaller images and fewer scripts.