To enable mod_rewrite (URL rewriting) for your hosted website, follow these steps:
Step 1: Create or Edit Your .htaccess File
Log in to your cPanel account
Navigate to "File Manager" in the Files section
Go to your website's root directory (usually
public_htmlor a subfolder if your site is in one)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
Save the
.htaccessfileTest your website to ensure the rules are working as expected
Important Notes
Make sure mod_rewrite is enabled on the server (it usually is by default in cPanel)
Always back up your
.htaccessfile before making changesSyntax errors in
.htaccesscan make your site inaccessible - if this happens, delete or rename the file via FTP or File Manager to restore accessFor 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.