Home >Backend Development >PHP Tutorial >How to Rewrite URLs with .htaccess to Handle GET Variables in PHP?
Rewriting URLs with .htaccess to Handle GET Variables
When developing web applications, it's often desirable to remove the index.php file extension from URLs. This can be achieved using .htaccess files and rewrite rules. In this case, you are trying to rewrite URLs like "http://localhost/controller/param/value/param/value" to "http://localhost/controller/?param=value¶m=value".
To accomplish this, you need to modify your .htaccess file with the following RewriteRule:
RewriteRule ^(.*)$ index.php?params= [NC, QSA]
This rule captures everything after the domain name and assigns it to a variable named "params" in your URL query string. For example, the URL "http://localhost/controller/param/value/param/value" will be converted to "index.php?params=controller/param/value/param/value".
In your index.php file, you can then access the GET variables by exploding the "params" string. Here's an example code snippet:
$params = explode("/", $_GET['params']); for($i = 0; $i < count($params); $i+=2) { echo $params[$i] . " has value: " . $params[$i+1] . "<br />"; }
This code will output the GET variables and their values in a user-friendly format. By implementing these changes, you can effectively rewrite URLs and handle GET variables in your PHP applications.
The above is the detailed content of How to Rewrite URLs with .htaccess to Handle GET Variables in PHP?. For more information, please follow other related articles on the PHP Chinese website!