Article

How to Create a Rewrite Rule in cPanel

Eclipse Web Portal
Created 2025-12-02 11:15:18
Updated 2025-12-26 14:35:54
Views 427

Rewrite rules allow you to redirect or modify URLs using Apache’s mod_rewrite module. In cPanel, rewrite rules are usually added to the .htaccess file. Common uses include redirecting pages, creating SEO‑friendly URLs, or forcing HTTPS.


Requirements

Before you begin, ensure:

  • Your hosting account uses Apache

  • mod_rewrite is enabled (this is enabled by default on most cPanel servers)

  • You have access to File Manager in cPanel


Step 1: Log in to cPanel

  1. Log in to your cPanel account.

  2. Navigate to Files → File Manager.


Step 2: Locate or Create the .htaccess File

  1. Open the directory where the rule should apply:

    • Usually public_html/

  2. If you do not see a .htaccess file:

    • Click Settings (top-right)

    • Enable Show Hidden Files (Dotfiles)

    • Click Save

  3. If the file still doesn’t exist:

    • Click + File

    • Name it .htaccess


Step 3: Enable Rewrite Engine

At the top of the .htaccess file, add:

RewriteEngine On

⚠️ This line should appear only once per file.


Step 4: Add a Rewrite Rule

Below are common examples you can copy and modify.

Example 1: Redirect One Page to Another

RewriteRule ^old-page\.html$ new-page.html [R=301,L]

Example 2: Force HTTPS

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Example 3: Remove .php from URLs

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php [L]

Example 4: SEO‑Friendly URLs

Convert:

example.com/profile.php?user=john

To:

example.com/profile/john
RewriteRule ^profile/([^/]+)/?$ profile.php?user=$1 [L,QSA]

Step 5: Save and Test

  1. Click Save Changes

  2. Test the URL in your browser

  3. Clear your browser cache if needed


Troubleshooting

  • 500 Internal Server Error:
    A syntax error may exist in .htaccess. Revert your changes and try again.

  • Rule not working:
    Ensure RewriteEngine On is present and rules are placed above other redirects.


Best Practices

  • Always back up .htaccess before editing

  • Use 301 redirects for permanent URL changes

  • Keep rules simple and well‑commented


Need Help?

If you are unsure which rewrite rule to use, contact your hosting provider or consult the Apache mod_rewrite documentation.