Skip to content
PHP Logo

What are the potential issues with using mod_rewrite to hide PHP variables in URLs?

One potential issue with using mod_rewrite to hide PHP variables in URLs is that it can make the code less readable and harder to maintain. To solve this issue, you can use a front controller pattern where all requests are routed through a single PHP file which then handles the logic to retrieve and process the variables.

php
// .htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

// index.php
<?php
$url = isset($_GET['url']) ? $_GET['url'] : '';
// Parse the URL and extract variables as needed