Advanced URL Routing with cPanel Mod_Rewrite

A website's public URLs do not have to mirror its underlying files. With Apache's mod_rewrite module, a visitor can open /product/42 while the server processes product.php?id=42 behind the scenes. Using rewrite rules in your site's .htaccess file, you can create cleaner URLs, redirect outdated addresses, and organize your website without restructuring every PHP file.
What Is Mod_Rewrite?
Mod_rewrite is a translation engine for links. A visitor requests /product/42, and the server quietly runs product.php?id=42, while the address bar stays clean. That turns an unreadable address like domain.com/product.php?id=42 into domain.com/product/42. Rewrite rules can be defined in Apache's server configuration or in .htaccess files. On shared cPanel hosting, .htaccess is usually the most accessible way to configure them. A RewriteCond line sets a condition, and a RewriteRule line matches a pattern with a regular expression (regex) and names the target.
Why It Matters
Google advises readable words instead of long ID numbers, hyphens instead of underscores, and fewer parameters. Descriptive paths are also easier to read, remember, and share. Rewriting works best with a 301 redirect from the old addresses, so visitors and search engines reach the new ones and old links keep working.
Activating the Engine
In cPanel File Manager, open Settings and enable Show Hidden Files (dotfiles).
Open .htaccess in your document root, usually public_html, or create it.
Copy it as a backup first.
Add RewriteEngine On before your rewrite rules if rewriting is not already enabled. Preserve existing CMS, PHP, security, and hosting-generated directives. If you are using WordPress or another CMS, place custom rules outside automatically managed configuration blocks and before any conflicting catch-all routing rules.
Writing a Clean Rule
Suppose your website currently displays products using URLs such as example.com/product.php?id=42. You want visitors to access the same content through example.com/product/42 without changing how your PHP application retrieves the product.
Add the following rules to the .htaccess file in your website's document root:
RewriteEngine On
# Redirect old product URLs to the clean URL
RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
RewriteCond %{THE_REQUEST} \s/+product\.php[?]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^product\.php$ /product/%1 [R=302,L,QSD]
# Internally route clean URLs to the PHP script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/([0-9]+)/?$ product.php?id=$1 [END]
The first rule redirects visitors who request the original PHP address to the cleaner URL. THE_REQUEST ensures that an internal rewrite does not trigger the redirect again, while QSD removes the original query string.
The second rule performs the internal routing. When someone visits /product/42, Apache processes product.php?id=42 without changing the address displayed in the browser.
The [0-9]+ pattern restricts product IDs to numbers, while the !-f and !-d conditions protect existing files and directories from being rewritten.
Start with a temporary 302 redirect while testing. Once you confirm that everything works correctly, change [R=302,L,QSD] to [R=301,L,QSD] to make the redirect permanent.
This example assumes that product.php is located in your website's document root and that the application uses numeric product IDs. Websites that depend on additional query parameters, such as filtering or campaign tracking, will need customized rules to preserve them. Choose a consistent trailing-slash format for your internal links and canonical URLs.
Common Mistakes
Wrong rule order. Place specific routing rules before broad catch-all rules. Use !-f and !-d conditions where appropriate to protect existing files and directories.
Using permanent redirects before testing. Start with a 302 redirect. Switch to a 301 only after confirming that the destination works correctly.
Conflicting rewrite rules. Existing HTTPS redirects, WordPress rules, or application routing can interfere with custom rules and cause redirect loops.
Ignoring query parameters. Some applications depend on additional parameters for filtering, tracking, or functionality. Make sure your rewrite rules preserve the information your application needs.
Rewriting every PHP file. Broad rules can unintentionally affect login pages, API endpoints, and form submissions. Use targeted rules wherever possible.
Testing with cached redirects. Browsers can cache permanent redirects. Use a private browser session or a command-line tool such as curl to inspect the actual server response.
Editing without a backup. A syntax error in .htaccess can make your entire website inaccessible. Keep a backup that you can restore through File Manager.
What This Means for You
With NameSilo's cPanel hosting, you can manage custom URL routing directly through File Manager. Start by backing up your .htaccess file, introduce one routing change at a time, and test both the original and rewritten URLs before making redirects permanent.
Once your new URLs are working, update your internal links, XML sitemap, and canonical tags where applicable. This helps visitors and search engines consistently discover the preferred addresses.
Our guide to forcing HTTPS demonstrates another practical use of rewrite rules. You can also explore our cPanel hosting plans if you need a hosting environment that gives you direct control over website configuration.
Frequently Asked Questions
What is mod_rewrite?
A module that rewrites incoming URLs to other paths using rules in .htaccess.
How do I turn on RewriteEngine?
Add RewriteEngine On before your rewrite rules in .htaccess. The mod_rewrite module must already be enabled on the web server.
Where is the htaccess file in cPanel?
In your document root, usually public_html. Enable Show Hidden Files in File Manager settings.
How do I remove .php from my URLs?
Use an internal rewrite to map a clean URL to its corresponding PHP file, and a separate external redirect to direct visitors from the original address to the new one. Test rules individually rather than automatically rewriting every PHP file.
Does mod_rewrite improve SEO?
Not directly. It allows you to create readable URLs and manage permanent redirects, which can support better website organization and consistent indexing. Removing file extensions alone does not improve search rankings.
Why is my htaccess redirect not working?
Common causes include incorrect syntax, conflicting rules, incorrect document roots, cached redirects, or server configuration restrictions.
What is the difference between a rewrite and a redirect?
An internal rewrite changes how the server processes a request without changing the browser's address. An external redirect instructs the browser to request a different URL.
NameSilo Staff
The NameSilo staff of writers worked together on this post. It was a combination of efforts from our passionate writers that produce content to educate and provide insights for all our readers.


