Skip to content
PHP Logo

How can mod_rewrite be utilized in PHP to enhance the URL structure and pass variables between pages?

Mod_rewrite can be utilized in PHP to enhance the URL structure by rewriting URLs in a more user-friendly format. This can also help in passing variables between pages without exposing them directly in the URL. By using mod_rewrite rules in the .htaccess file, we can rewrite URLs to make them more readable and SEO-friendly, while still passing variables to the PHP scripts behind the scenes.

php
```php
RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [NC,L]
```

In this example, a URL like "example.com/products/123" will be rewritten internally to "example.com/product.php?id=123", allowing us to access the product ID variable in the PHP script without exposing it in the URL.