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.

 

Why Use mod_rewrite?

  • 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).

 

Enabling mod_rewrite

  • On most Ruachost shared hosting accounts, mod_rewrite is already enabled.

 

Basic Example: Clean Product URLs

Instead of:

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

 

You can rewrite to:

http://www.example.com/products/calculator

.htaccess Rule:

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.

 

Common Use Cases

1. Redirect to HTTPS

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

2. Remove .php Extension

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

3. Force Trailing Slash

 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Testing Your Rules

  • Use browser developer tools to confirm redirects.

  • Check Apache error logs if rules don’t work.

  • Avoid infinite loops by testing carefully.

Important Notes

  • Place rules in the .htaccess file in your site’s root (public_html).

  • Order matters — rules are processed top to bottom.

  • Always back up .htaccess before editing.

  • Misconfigured rules can cause 500 Internal Server Errors.

 
Je li Vam ovaj odgovor pomogao? 0 Korisnici koji smatraju članak korisnim (0 Glasovi)

Powered by WHMCompleteSolution