Every rule here runs on Apache and on LiteSpeed without changes. Paste one block at a time, keep a copy of the working file, and test in a private window.
Build your .htaccess
Runs in your browserPlace the file in your site's public root (merge into an existing file rather than replacing rules you already run). Every plan that runs Apache honours these directives — see Linux hosting and shared hosting.
What an .htaccess generator produces
An .htaccess file holds per-directory directives, and the server reads it on every request below that directory. A generator's job is to pick the right directive and flags for the job you described.
| Job | Directive | Needs |
|---|---|---|
| Redirect one path | Redirect 301 /old /new | mod_alias |
| Redirect a pattern | RewriteRule with [R=301,L] | mod_rewrite |
| Force HTTPS | RewriteCond %{HTTPS} off plus a rule | mod_rewrite |
| Browser caching | ExpiresByType | mod_expires |
| Compression | AddOutputFilterByType DEFLATE | mod_deflate |
| Custom error page | ErrorDocument 404 /404.html | core |
| IP allow or deny | Require ip / Require not ip | mod_authz_core |
If a module is missing, the directive either fails or returns 500, which is why working files wrap optional blocks in an IfModule test for that module.
Redirect rules: 301, 302 and when each applies
A 301 says the move is permanent and passes ranking signals to the new URL. A 302 says the change is temporary and keeps the old URL as the canonical one, so using 302 for a real migration is a quiet SEO loss.
- Single page —
Redirect 301 /old-page /new-page - Folder or pattern —
RedirectMatch 301 ^/blog/(.*)$ /articles/$1 - Temporary — the same lines with 302 while a campaign or test page is live.
Point the target at the final URL, not at another redirect. A chain of 301s slows every visit and Google follows only a limited number of hops. On WebJi's LiteSpeed shared hosting the same directives behave as they do on Apache.
RewriteRule patterns, flags and RewriteBase
A RewriteRule matches the path after the domain, without the leading slash, and rewrites it. The flags in square brackets decide the status code, whether processing stops, and whether matching is case sensitive.
- RewriteEngine On — required once at the top; without it every rule is ignored.
- [L] — stop processing further rules once this one matches.
- [R=301] — send a redirect instead of an internal rewrite.
- [NC] — case-insensitive match, useful for legacy URLs typed in capitals.
- [QSA] — append the original query string to the target.
- RewriteBase / — set it when the site lives in a subdirectory, or the rewritten path resolves from the wrong place.
A pretty-URL rule usually ends with a front-controller fallback, for example RewriteCond %{REQUEST_FILENAME} !-f then RewriteRule . /index.php [L]. Without the condition, real images and CSS files get routed through PHP as well.
Browser caching and compression rules
Cache headers decide how long a browser keeps an image, stylesheet or script before asking again. Compression shrinks text responses in transit. Both cut load time without touching the page content.
- Images —
ExpiresByType image/webp "access plus 1 year", safe because the file name should change when the image does. - CSS and JS — a week is a common compromise while a site is changing often.
- HTML — keep it short, or a deploy will not show up for returning visitors.
- Compression —
AddOutputFilterByType DEFLATE text/html text/css application/javascript.
Put both blocks inside an IfModule test for mod_expires and mod_deflate. A server without the module then skips the block instead of returning 500 for the whole site.
Common .htaccess syntax errors and how to recover
Every page returning 500 after an edit means the file, not the site, is broken. The error log names the line, and restoring the backup takes seconds if you kept one.
- Missing RewriteEngine On — rules look correct and do nothing at all.
- Unclosed quote — ErrorDocument and Redirect values need matching quotes when they contain spaces.
- Deny from on Apache 2.4 — replaced by Require; the old form needs mod_access_compat and can fail without an obvious message.
- Windows line endings — a file saved with CRLF can produce a syntax error; save as Unix or UTF-8 without BOM.
- Wrong order — a catch-all rule above a specific one swallows it, because processing stops at the first match with [L].
- No RewriteBase in a subdirectory — the rewrite resolves against the server root instead of the site folder.
Recovery is the same every time: restore the previous file, confirm the site loads, then paste back one block and test before adding the next.
Where the .htaccess file goes and how to test a change
The file belongs in the directory it should govern, normally the site root at public_html. Its name is exactly .htaccess, with a leading dot and no extension, and many file managers hide dotted files until you enable hidden items.
- Download the current .htaccess as a backup before editing anything.
- Paste one block, save as plain text, and upload in the same encoding.
- Open the site in a private window and request the old URL to confirm the status code.
- Check the server error log if a page fails; it names the broken directive.
- Keep the backup until the change has survived a full day of traffic.
If the site runs on Nginx, none of this applies, since Nginx ignores the file; the VPS hosting page covers servers where you control the web server config directly.
Who writes .htaccess rules by hand
Site owners on Apache or LiteSpeed who need one redirect, one cache header or one IP block, and developers handing over a site whose rules the next person has to read. A commented rule is worth more than a clever one.
- You are moving a site and must keep old URLs working.
- You need HTTPS or a www choice enforced across every path.
- You are blocking a scraper or a referrer spam range.
Why WebJi® for .htaccess help
- Linux plans run LiteSpeed, so the rules above apply exactly as written.
- In business since 2005, with 12,000+ clients across India and overseas.
- 24/7 email support in Hindi and English, average first reply under 60 minutes.
- Support reads a broken .htaccess with you instead of asking you to reinstall.
Send the file and the URL that fails through the contact page and we will find the line that is breaking it.
Frequently Asked Questions
How do I create a .htaccess file?
Create a plain-text file named exactly .htaccess in the site root, usually public_html, with no extension before or after the name. Paste one block of rules, save without a byte-order mark, and upload it; Apache reads it on the next request with no restart.
How do I redirect a page using .htaccess?
For one path use Redirect 301 /old-page /new-page. For a pattern use RewriteEngine On followed by RewriteRule ^old/(.*)$ /new/$1 [R=301,L]. Test in a private window and confirm the status is 301 rather than 302.
How do I force HTTPS in .htaccess?
Turn the engine on, test whether %{HTTPS} is off in a RewriteCond, then redirect to https://%{HTTP_HOST}%{REQUEST_URI} with [R=301,L]. Place it before every other rewrite so it runs first.
What is hotlink protection?
It stops other sites from loading your images by checking the Referer header. Allow your own domain and an empty referer, otherwise direct visits and some feed readers will show broken images.
How do I block an IP in .htaccess?
On Apache 2.4 use Require not ip 203.0.113.5 inside a RequireAll block. The older Deny from syntax depends on mod_access_compat and fails silently on a modern server, which is why an IP block sometimes appears to do nothing.
Why did my site break after editing .htaccess?
A syntax error returns 500 for every page in that directory. Restore the backup by file manager or FTP, then re-add one block at a time; the error log names the line that failed and the reason.
Does .htaccess work on Nginx?
No. Nginx does not read .htaccess at all, so the same rules have to become server-block directives. On Apache and on LiteSpeed the file works as written.

