Enable Apache Mod Rewrite

  • May 14, 2025
  • 0 Comments

To enable mod_rewrite (URL rewriting) for your hosted website, follow these steps:

Step 1: Create or Edit Your .htaccess File

  1. Log in to your cPanel account

  2. Navigate to "File Manager" in the Files section

  3. Go to your website's root directory (usuallypublic_htmlor a subfolder if your site is in one)

  4. Either:

    • If.htaccessexists, right-click and select "Edit"

    • If it doesn't exist, click "New File" and name it.htaccess

Step 2: Add mod_rewrite Rules

Add these lines to your.htaccessfile:

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Your rewrite rules go here
    # Example: RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [NC,L]
</IfModule>

Step 3: Common Configuration Options

To remove .php extensions:

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

To force HTTPS:

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

To remove www:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Step 4: Save and Test

  1. Save the.htaccessfile

  2. Test your website to ensure the rules are working as expected

Important Notes

  1. Make sure mod_rewrite is enabled on the server (it usually is by default in cPanel)

  2. Always back up your.htaccessfile before making changes

  3. Syntax errors in.htaccesscan make your site inaccessible - if this happens, delete or rename the file via FTP or File Manager to restore access

  4. For WordPress sites, the default.htaccessrules are usually sufficient

If you encounter 500 Internal Server errors after editing.htaccess, check your syntax carefully or revert to a previous version.

How helpful was this article to you?

Posting has been disabled.