How can mod_rewrite in PHP be effectively used to redirect URLs with parameters to cleaner, more user-friendly URLs?
When using mod_rewrite in PHP, you can effectively redirect URLs with parameters to cleaner, more user-friendly URLs by creating rewrite rules in your .htaccess file. This allows you to mask complex URLs with parameters and present a simplified and more user-friendly version to your visitors.
```php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [NC,L]
```
In this example, the code snippet redirects a URL like `example.com/products/123` to `example.com/product.php?id=123`, providing a cleaner URL structure for users while maintaining the functionality of passing parameters to the PHP script.
Related Questions
- What is the recommended approach for including static (html) files in PHP, and how does it differ from including PHP files?
- When developing a custom online shop in PHP, what are the key security measures that should be implemented to prevent hacking attempts?
- What are the potential pitfalls of editing PHP code for button customization?