Decoding .htaccess Rewrites for Multi-Segment GET Variables
You have a standard routing setup where index.php?page=controller maps cleanly to a specific controller file. Your current configuration uses two negative lookahead conditions (!-f, !-d) to ensure that actual files and directories aren't rewritten, followed by a rule that captures a single segment of the URL path and passes it as the page GET variable. This is a classic "front controller" pattern, but it hits a wall when you attempt to map complex URLs like /controller/param/value/param/value into query strings like ?action=delete&x=something.
The fundamental issue lies in how Apache's RewriteRule processes the URL path versus the query string. Your existing regex ^([\w\d~%.:_\-]+)$ defines a strict boundary around the first segment of the path. It stops reading as soon as it hits the second slash. Consequently, the /param/value/... portion remains in the physical URL path rather than being absorbed into the request's query string as expected by your PHP $_GET array.
To achieve the desired behavior where /controller/action/delete becomes index.php?page=controller&action=delete, you must shift the logic from capturing a single path segment to either stripping the entire path suffix or explicitly reconstructing the query string based on path segments. However, there is a critical architectural decision to make here regarding how your controller receives these parameters.
If your goal is to parse /controller/param/value into $_GET['param'] => 'value', standard Apache mod_rewrite does not automatically split arbitrary path segments into GET variables. It treats the entire string after ? as a single query string unless you explicitly define it in the rule. A common approach involves rewriting the URL to append everything after the controller name as a raw query string, which your PHP script can then parse.
Consider modifying your rule to capture everything following the controller name and pass it directly into the query string:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Capture the controller name, but pass the rest of the path as a raw query string
RewriteRule ^([\w\-]+)(.*)$ index.php?page=$1&query=$2 [L, QSA]
In this configuration, a request to http://localhost/controller/delete/something would be rewritten to index.php?page=controller&query=delete/something. Your PHP controller then needs to handle the parsing logic. You can inspect $_GET['query'] and use explode() or custom routing logic to break that string into individual arguments if your framework doesn't support native path-to-query translation.
However, a significant trade-off exists with this approach: URL encoding. If you want /controller/param/value to map strictly to ?action=delete&x=value, Apache alone cannot infer the variable names (action, x) from the path segments without complex regex that is difficult to maintain and prone to breaking on edge cases like special characters in paths.
A more robust solution often involves using a dedicated routing library within your PHP application rather than relying solely on .htaccess for semantic URL mapping. Apache is excellent at rewriting URLs to canonical forms (e.g., hiding the index.php file), but parsing arbitrary path segments into named query parameters is generally an application-level concern.
If you strictly need to handle multiple GET variables derived from the path, ensure that your PHP code explicitly reconstructs the $_GET array from the query string if it arrives in that format:
if (isset($_GET['query'])) {
// Simple split for demonstration; use a router for production complexity
$parts = explode('/', $_GET['query']);
$_GET['action'] = $parts[0];
$_GET['x'] = $parts[1] ?? null;
}
This pattern keeps your .htaccess file clean and focused on routing the request to index.php, while delegating the complexity of argument parsing to your application logic. For modern Laravel projects, consider leveraging Laravel's built-in routing capabilities which handle these mappings natively without requiring complex .htaccess manipulation. If you are maintaining a legacy setup similar to the example provided, understanding the distinction between path rewriting and query string reconstruction is vital for avoiding fragile regex rules that break with dynamic content.