.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

Address
Application
Security
Speed
301 redirects
.htaccess

  

Where the .htaccess file goes on cPanel

  1. Open cPanel → File Manager.
  2. Click Settings (top right) and tick Show Hidden Files (dotfiles). Without this you will not see the file at all.
  3. Go to the folder of the site: public_html for the main domain, or the add-on domain's own folder.
  4. If a .htaccess is 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 matching RewriteCond %{QUERY_STRING}, and the old query string is not carried over to the new address.

If the site shows a 500 error after saving

  1. Restore your old file right away (that is why you downloaded a copy).
  2. Check cPanel → Errors or the error_log file; it names the line Apache did not accept.
  3. 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, except Options -Indexes on a few very locked-down hosts. Remove that one line if it is the culprit.

FAQ

Does this work on LiteSpeed hosting (Namecheap, Hostinger)?
Yes. LiteSpeed reads Apache .htaccess rules, including mod_rewrite, headers and expires. The rules here are standard Apache syntax.
Will a 301 redirect lose my Google rankings?
A 301 passes the old page's ranking signals to the new URL. Redirect each old page to its closest new page, not everything to the home page, and update internal links so visitors do not go through redirects at all.
Should I turn on HSTS?
Only when HTTPS works on the main domain and every subdomain. HSTS tells browsers to refuse plain HTTP for a year, and you cannot undo that for visitors who already received it.
My Laravel API returns 401 even with a token. Why?
On some Apache setups the Authorization header never reaches PHP. The front-controller option includes the two lines Laravel ships to pass it through (HTTP_AUTHORIZATION).
Where do I put this for WordPress?
In the WordPress folder, usually 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.

Found it useful? Put a link in your docs, README or forum answer so others can use it too:

HTML
<a href="https://headsntail.com/tools/htaccess-generator">.htaccess Generator for cPanel</a> (free, by HeadsNTail)
Markdown (GitHub, Stack Overflow, dev.to)
[.htaccess Generator for cPanel](https://headsntail.com/tools/htaccess-generator)

Need it done on your server? HeadsNTail sets up Laravel apps, cPanel hosting and migrations for businesses. See services or ask a question.