Skip to main content
NyankoTools

.htaccess Generator (Redirects, Force HTTPS, www Canonical)

Build an Apache .htaccess file by choosing the rules you need: force HTTPS, canonical www or non-www host, trailing slashes, per-URL 301/302 redirects, and custom error pages. The tool flags redirect loops and duplicates, and tells you how to verify the result after you upload it. For what each redirect code means, see the HTTP Status Codes reference.

🔒 Runs in your browser · Your data is never sent to a server

Read this before you deploy

A mistake in .htaccess can make your whole site return a 500 error. Back up your current .htaccess before changing it, open the site right after the change, and restore the backup immediately if it breaks.

HTTPS

If a proxy terminates HTTPS and you leave this unchecked, the site can end up in an endless redirect loop.

www or non-www

Path normalization

Per-URL redirects

The old path starts with /. The destination is a path starting with / or a URL starting with https://. The status can be 301 (permanent), 302 (temporary), 307 or 308, and defaults to 301. Lines starting with # are ignored.

Other

Error pages

Checks

    Browsers drop the leading dot, so the file is saved as htaccess.txt. Rename it to .htaccess before you put it on the server.

    Checklist after deploying

    1. Before the change, copy the existing .htaccess on the server to a backup name (copying it in FTP or the file manager is enough).
    2. Add the generated content to the .htaccess in your site root, or replace it. If a CMS such as WordPress already wrote rules there, add yours above the # BEGIN WordPress line.
    3. Open the top page in a browser and check there is no 500 Internal Server Error. If there is, restore the backup from step 1 right away.
    4. Run the command below to check that the Location and status (such as 301) are what you expect and that the redirects do not repeat many times.
    curl -sIL http://example.com/ | grep -iE "^(HTTP|Location)"

    How to use

    1. Choose the rules you want, such as "Force HTTPS", "Unify www" and "Trailing slash".
    2. If you unify www, enter the bare domain, such as example.com, in the "Domain" field.
    3. To forward individual URLs, enter one "old path destination" pair per line in "Per-URL redirects".
    4. Review the checks, then use "Copy" or "Download" and apply it to the .htaccess in your site root (rename a downloaded htaccess.txt to .htaccess).

    Notes

    • .htaccess works only on Apache and compatible servers. It does not work on nginx or on static hosts such as Cloudflare Pages, Netlify or Vercel.
    • If the server does not allow AllowOverride, .htaccess is ignored, or directives such as Options cause a 500 error. Some shared hosts also provide an .htaccess editor in their control panel.
    • The generated rules use mod_rewrite and mod_alias and are wrapped in <IfModule> so a missing module does not cause a 500 error. The downside is that the redirect then silently does nothing, so always test it after deploying.
    • The rules assume the file lives in the site root (document root). If you place it in a subdirectory, the paths need adjusting.
    • A CMS such as WordPress may rewrite the same .htaccess. Add your rules outside the section the CMS manages (between its # BEGIN and # END lines).
    • Browsers and search engines cache a 301 as a permanent move, which is hard to undo. Try a 302 first until you are sure it works.
    • Apache applies per-URL redirects before the force-HTTPS and www rules, so a request to an http:// or www URL can be redirected twice. To do it in one step, make the destination a full https:// URL on your canonical host.
    • Hand-written regular expressions are not supported. For complex conditions, edit the generated output by hand.

    Glossary

    .htaccess

    A per-directory configuration file for Apache. It lets you set redirects, access rules and more without changing the main server configuration.

    301 / 302 redirect

    301 means a permanent move and 302 a temporary one. Search engines treat a 301 as passing ranking signals to the new URL. 307 and 308 forward while keeping the request method (such as POST).

    mod_rewrite / mod_alias

    Apache modules. mod_rewrite does conditional rewriting (force HTTPS, www canonicalization), while mod_alias handles plain URL redirects (Redirect).

    AllowOverride

    An Apache setting that decides which kinds of directives .htaccess may override. If it is not allowed, the .htaccess rules are ignored or cause a 500 error.

    FAQ

    Where do I put the .htaccess file?

    Save it as .htaccess directly in your public folder (the document root, such as public_html). These rules assume the root .htaccess that covers the whole site. A file in a subdirectory affects only that directory and the paths in the rules would need to change.

    After enabling Force HTTPS my browser says there were too many redirects.

    When a CDN or reverse proxy such as Cloudflare terminates HTTPS, the request reaches your server over plain HTTP, so the HTTPS check always fails and the redirect repeats. Tick "Site is behind a CDN or reverse proxy" so the rule also reads the X-Forwarded-Proto header. If your CDN has its own "Always use HTTPS" option, you can also let it do the job instead of .htaccess.

    Should I use a 301 or a 302 redirect?

    Use 301 when a URL has moved for good and 302 for a temporary forward such as a limited-time campaign. Browsers and search engines cache a 301, which makes it hard to undo, so start with 302 while testing and switch to 301 once you are sure.

    What is the difference between exact and prefix matching?

    Exact matching forwards only the URL /old. Prefix matching also forwards everything under it, so /old/page goes to /new/page. Use prefix matching when you move a whole directory and exact matching for a single page. With prefix matching, a destination inside the old path would loop forever, so the tool reports it as an error.

    My site shows a 500 error after the change. What now?

    Restore the .htaccess you backed up before the change right away. A common cause is that the server does not allow some directives, such as Options, through AllowOverride. In that case, untick "Disable directory listings" and try again, or check whether your hosting control panel offers the same setting.

    Share this page

    ↑↓ Select Enter Open Esc Close