Current Location: Home> Latest Articles> Use output_add_rewrite_var in PHP to generate a friendly URL with rewrite_rules

Use output_add_rewrite_var in PHP to generate a friendly URL with rewrite_rules

gitbox 2025-06-05

In PHP development, generating user-friendly URLs (so-called SEO URLs or pseudo-static URLs) is a common requirement. It not only improves the user experience, but also helps search engines inclusion. Most developers will choose to use Apache's mod_rewrite or Nginx's rewrite function to implement it, but PHP itself also provides some auxiliary mechanisms, such as output_add_rewrite_var() , which can work in specific scenarios, especially when you are using a query string-based system and want to achieve pseudo-static effects.

This article will explain how to use output_add_rewrite_var() with the URL rewrite rules of the Web server to achieve user-friendly URL output.

What is output_add_rewrite_var()

output_add_rewrite_var() is an output control function provided by PHP, which can automatically append the specified query parameters to all URLs output through echo , print , or templates (including <a> tags and forms). This function is often used for automatic propagation of session IDs, and can also be used for other custom variables.

 <?php
output_add_rewrite_var('page', 'home');
?>

The above code will automatically append ?page=home or &page=home to all output URLs and forms.

Beautify URLs in conjunction with Rewrite rules

Although output_add_rewrite_var() can only add query strings, we can use the server-side rewrite function to convert this query string structure into a more elegant path format. For example, put:

 https://gitbox.net/index.php?page=home

Convert to:

 https://gitbox.net/home

This is achieved through the server's rewrite rules.

Apache Example

 RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,QSA]

Nginx Example

 location / {
    try_files $uri $uri/ /index.php?page=$uri;
}

PHP processing logic

Then in index.php you can load the corresponding content according to the value of $_GET['page'] :

 <?php
$page = $_GET['page'] ?? 'home';

switch ($page) {
    case 'home':
        include 'pages/home.php';
        break;
    case 'about':
        include 'pages/about.php';
        break;
    default:
        include 'pages/404.php';
}
?>

In this way, visiting https://gitbox.net/about will actually be rewritten as index.php?page=about , and the PHP script can recognize and load the corresponding page.

Automatically append link parameters using output_add_rewrite_var()

To further automate the query parameters of the URL (such as paging parameters, language parameters, etc.), you can use:

 <?php
output_add_rewrite_var('lang', 'zh');
?>

This will automatically change the link in the page to:

 https://gitbox.net/about?lang=zh

In conjunction with the rewrite rules, you can further rewrite the parameters to make them:

 https://gitbox.net/zh/about

Just add a layer of language recognition to the server rules.

summary

Although modern frameworks (such as Laravel and Symfony) provide more modern and structured routing solutions, in some lightweight or custom development projects, PHP native output_add_rewrite_var() is still a feasible way to generate user-friendly URLs in some lightweight or customized development projects. Mastering this method allows you to control the URL structure of your project more flexibly without relying entirely on external frameworks.

In addition, this approach is especially useful for legacy projects, and does not require a significant change in the code structure to achieve better URL performance.