Skip to content

Rewriting URLs with the mod_rewrite module

The mod_rewrite module in Apache allows you to rewrite URLs dynamically. On ruachost.com, you can use it to create cleaner, SEO‑friendly links, hide internal implementation details, and redirect visitors to secure connections.

  • SEO benefits: Search engines prefer human‑readable URLs.
  • Security: Hide query strings and internal file structures.
  • User experience: Provide simple, memorable links.
  • Flexibility: Redirect traffic based on conditions (e.g., HTTPS enforcement).
  • On most Ruachost shared hosting accounts, mod_rewrite is already enabled.

Instead of:

http://www.example.com/products.php?id=232&cat=11

You can rewrite to:

http://www.example.com/products/calculator
RewriteEngine On
RewriteRule ^products/calculator$ /products.php?id=232&cat=11 [L]
  • RewriteEngine On → enables rewriting.
  • RewriteRule → matches incoming URL and maps it to the target file.
  • [L] → stops processing further rules once matched.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
  • Use browser developer tools to confirm redirects.
  • Check Apache error logs if rules don’t work.
  • Avoid infinite loops by testing carefully.