Home Backend Development PHP Tutorial How Can I Rewrite URLs with GET Variables in .htaccess for Clean Routing?

How Can I Rewrite URLs with GET Variables in .htaccess for Clean Routing?

Nov 15, 2024 am 11:34 AM

How Can I Rewrite URLs with GET Variables in .htaccess for Clean Routing?

Rewriting URLs with GET Variables in .htaccess

When using PHP for routing purposes (e.g., index.php?page=controller), it is useful to have a cleaner and more user-friendly URL structure. .htaccess, a widely used Apache configuration file, provides a convenient way to rewrite URLs and handle GET variables.

In the given situation, the goal is to have URLs like http://localhost/controller/ route to index.php?page=controller in a clean and concise manner. Additionally, the requirement is to support URLs with parameters, such as http://localhost/controller/param/value/param/value, which should map to http://localhost/controller/?param=value&param=value with the GET variables.

To achieve this, a simple yet effective solution can be implemented using a rewrite rule in the .htaccess file:

RewriteRule ^([\w\d~%.:_\-]+)/(.+)$ index.php?page=&params= [NC, QSA]
Copy after login
  1. Rewrite Pattern: The RewriteRule starts with the pattern ^([wd~%.:_-] )/(. )$, which matches any URL consisting of a controller name followed by a slash and any additional parameters separated by slashes.
  2. Target URL: The target URL is index.php?page=$1&params=$2. Here, $1 represents the controller name, and $2 represents the entire parameter string.
  3. Rewrite Flags: The NC flag (No Case) ensures case-insensitive matching, while the QSA flag (Query String Append) concatenates the existing GET string with the parameters from the rewrite rule.

With this rule in place, a URL like http://localhost/controller/param/value/param/value will be rewritten to index.php?page=controller&params=param/value/param/value. This provides a clean and easy way to manage GET parameters and maintain the original URL structure.

To access these parameters in your PHP code, you can use $_GET['params'], which contains the parameter string. You can then split it using a delimiter (e.g., /) and access individual parameter values.

The above is the detailed content of How Can I Rewrite URLs with GET Variables in .htaccess for Clean Routing?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles