.htaccess is a small configuration file that controls how your website answers requests: redirects, clean URLs, caching and access rules. It is hidden by default because its name starts with a dot. This guide shows how to find and edit it in cPanel and gives tested rewrite examples.
Before you start
- Access to cPanel.
- A basic understanding that rules are read from top to bottom and that one mistake can return a 500 error for the whole site.
Show and open .htaccess
- Open cPanel → Files → File Manager.
- Click Settings at the top right, tick Show Hidden Files (dotfiles) and click Save.
- Open
public_html(or the document root of an addon domain). - Right-click
.htaccessand choose Download to keep a backup. - Right-click it again and choose Edit. If the file does not exist, click + File, name it
.htaccessand create it. - Make your changes and click Save Changes.
Warning: WordPress and other apps manage the block between # BEGIN WordPress and # END WordPress. Put your own rules above that block, or they may be overwritten.
Create friendly URLs
Turn /product.php?id=25 into /product/25. The visitor sees the clean URL while the server still runs the script:
RewriteEngine On
RewriteRule ^product/([0-9]+)/?$ product.php?id=$1 [L,QSA]Send every request that is not a real file or folder to a front controller, as most PHP frameworks expect:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]Redirect a page or the whole site
Permanent redirect for one moved page:
Redirect 301 /old-page.html https://example.com/new-page/Move an entire site to a new domain, keeping the path:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]For simple cases you can use the redirect tool instead; see redirects in cPanel.
Force www or non-www
Pick one version and redirect the other, so search engines index only one:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]To force HTTPS as well, see force HTTPS in cPanel.
Check the result
- Test each rule in a private browser window, since browsers cache 301 redirects.
- Use
R=302while testing, then change toR=301once it works.
Troubleshooting
- 500 Internal Server Error: upload your backup or remove the last rule. Check cPanel → Metrics → Errors for the message.
- Too many redirects: two rules send visitors back and forth; see fix ERR_TOO_MANY_REDIRECTS.
- CSS and images break with clean URLs: use absolute paths starting with
/in your HTML.
