cPanel

Edit .htaccess in cPanel and create friendly URLs with rewrite rules

Show and edit the hidden .htaccess file in cPanel File Manager, then use mod_rewrite to build clean URLs, redirect pages and force www or non-www safely.

IntermediatecPanel hosting2 min read

.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

  1. Open cPanel → Files → File Manager.
  2. Click Settings at the top right, tick Show Hidden Files (dotfiles) and click Save.
  3. Open public_html (or the document root of an addon domain).
  4. Right-click .htaccess and choose Download to keep a backup.
  5. Right-click it again and choose Edit. If the file does not exist, click + File, name it .htaccess and create it.
  6. 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=302 while testing, then change to R=301 once 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.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.