UnderHost
Knowledgebase Docs

Advanced .htaccess: redirects, PHP, and access rules

Edit .htaccess safely in cPanel for redirects, custom errors, access control, MIME types, caching, and PHP settings without causing 500 errors.

On this page

The .htaccess file controls Apache behavior for a directory and its subdirectories. It can handle redirects, access rules, custom errors, MIME types, caching, and some PHP settings, but a single syntax mistake can produce a 500 error.

Edit safely

  1. Open cPanel File Manager.
  2. Open Settings and enable Show Hidden Files.
  3. Go to the document root, such as public_html.
  4. Download or copy the existing .htaccess file before editing.
  5. Make one change at a time and test the site after each change.

If the file does not exist, create a new file named exactly .htaccess.

Redirects

# Redirect one URL
Redirect 301 /old-page/ https://example.com/new-page/

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

For simple redirects, cPanel Domains then Redirects is often easier. Use manual rules when you need more control.

Custom error pages

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
ErrorDocument 500 /500.html

Make sure the custom error page exists and does not rely on broken application code, or visitors may see another error.

Access rules

# Block access to a sensitive file

  Require all denied


# Block an IP address
Require all granted
Require not ip 203.0.113.10

For directory passwords, use cPanel Directory Privacy instead of writing authentication rules manually.

PHP settings

On many modern cPanel servers, PHP settings should be changed with MultiPHP INI Editor or Select PHP Version. Use .htaccess PHP directives only if your account/server supports them.

# Only if supported by the server's PHP handler
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value memory_limit 256M

Fix 500 errors after editing

  • Rename .htaccess temporarily to confirm whether it is the cause.
  • Restore the backup copy you made before editing.
  • Check cPanel Metrics then Errors for the exact bad directive.
  • Remove unsupported PHP directives if the error mentions php_value or php_flag.
  • Check for conflicting rules in nested directories.

Related: Redirects in cPanel: create, test, and remove URL redirects | Changing PHP version in cPanel (MultiPHP Manager) | How to enable PHP extensions in cPanel | IP Blocker in cPanel: block abusive visitors safely | .htaccess guide for cPanel hosting

Was this article helpful?

Need cPanel hosting or licensing?

Use UnderHost cPanel hosting or add cPanel/WHM to a VPS or dedicated server for familiar website, email, DNS, and database management.

Related articles

Back to cPanel