Home > Backend Development > PHP Tutorial > How to Rewrite URLs with .htaccess to Handle GET Variables in PHP?

How to Rewrite URLs with .htaccess to Handle GET Variables in PHP?

DDD
Release: 2024-11-19 03:15:02
Original
656 people have browsed it

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]
Copy after login

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 />";
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template