Home >Backend Development >PHP Tutorial >How to Rewrite GET Variables into Path Parameters using .htaccess?

How to Rewrite GET Variables into Path Parameters using .htaccess?

DDD
DDDOriginal
2024-12-11 07:17:09591browse

How to Rewrite GET Variables into Path Parameters using .htaccess?

.htaccess Rewrite GET Variables with Path Parameters

Question:

How can I use .htaccess to rewrite GET variables into a URL path parameter format? Specifically, I want to convert:

to:

and also support parameters like:

to:

Answer:

To achieve this, modify your .htaccess file to include the following rewrite rule:

Explanation:

  • The ^ symbol matches the beginning of the URL.
  • The (.*) pattern matches everything after the beginning of the URL and assigns it to the $1 capture group.
  • The index.php is the target file to be rewritten.
  • The ?params=$1 is the new query string with the params parameter set to the captured $1 value.
  • [NC] flag makes the rule case-insensitive.
  • [QSA] flag appends the existing query string to the new one, preserving any additional parameters.

The above is the detailed content of How to Rewrite GET Variables into Path Parameters using .htaccess?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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