.htaccess Generator for cPanel Hosting
Tick what you need and copy the file. Every rule here was run on a real Apache server before publishing: HTTPS and www in one redirect, old URLs to new ones, .env blocked, Laravel without /public in the URL.
FreeNo sign-upRuns in your browser: your data is not uploaded
Where the .htaccess file goes on cPanel
- Open cPanel → File Manager.
- Click Settings (top right) and tick Show Hidden Files (dotfiles). Without this you will not see the file at all.
- Go to the folder of the site:
public_htmlfor the main domain, or the add-on domain's own folder. - If a
.htaccessis already there, download a copy first. Then edit it and paste the new rules.
Some lines in an existing file are added by cPanel itself, for example the # php -- BEGIN cPanel-generated handler block that selects the PHP version. Keep that block as it is and paste the generated rules above or below it.
One redirect instead of two
Many snippets force HTTPS first and remove www second. A visitor who types http://www.example.com then gets two redirects in a row, which is slower and wastes crawl budget. This generator checks www first and sends the visitor straight to the final https://example.com address in a single 301. We confirmed that on Apache with curl before publishing the tool.
The HTTPS rule also looks at the X-Forwarded-Proto header. Behind Cloudflare or a hosting proxy, Apache can see a plain HTTP connection even when the visitor uses HTTPS; without that check you get the "too many redirects" error.
Laravel on shared hosting without /public in the URL
The clean setup is to point the domain's document root to Laravel's public folder (cPanel → Domains → change the document root). If your host does not allow that and the whole project sits in public_html, tick Laravel uploaded to the account root. The rule passes every request into public/, and Laravel's own public/.htaccess takes it from there.
In that layout your .env, composer.json and storage folder sit inside the web root, so keep the two "block" options on. We tested that /.env and /composer.json return 403 with them.
Writing redirects
- Left box: the old path, for example
/old-page.html. A full URL works too; the tool keeps only the path. - Right box: the new path or a full URL on another domain.
- Old URLs with a query string, like
/index.php?page=about, get a matchingRewriteCond %{QUERY_STRING}, and the old query string is not carried over to the new address.
If the site shows a 500 error after saving
- Restore your old file right away (that is why you downloaded a copy).
- Check cPanel → Errors or the
error_logfile; it names the line Apache did not accept. - The usual cause is a module the host has not enabled. Every block here is wrapped in
<IfModule>, so missing modules are skipped instead of crashing the site, exceptOptions -Indexeson a few very locked-down hosts. Remove that one line if it is the culprit.
FAQ
Does this work on LiteSpeed hosting (Namecheap, Hostinger)?
Will a 301 redirect lose my Google rankings?
Should I turn on HSTS?
My Laravel API returns 401 even with a token. Why?
HTTP_AUTHORIZATION).Where do I put this for WordPress?
public_html. WordPress adds its own # BEGIN WordPress block; keep it and put these rules above it, leaving the front-controller option off because WordPress already has it.Link to this tool
Found it useful? Put a link in your docs, README or forum answer so others can use it too:
<a href="https://headsntail.com/tools/htaccess-generator">.htaccess Generator for cPanel</a> (free, by HeadsNTail)
[.htaccess Generator for cPanel](https://headsntail.com/tools/htaccess-generator)
Share: X LinkedIn Reddit WhatsApp
Need it done on your server? HeadsNTail sets up Laravel apps, cPanel hosting and migrations for businesses. See services or ask a question.