PHP output_add_rewrite_var() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Output Control

Definition

In PHP, output_add_rewrite_var() stores a name/value pair for the URL rewriter and starts the URL-Rewriter output handler if it is not already active.

When that handler flushes, it can add the pair as a query parameter on matching HTML URLs (and as hidden fields in forms) based on url_rewriter.tags and related ini settings. It is separate from ordinary ob_start() buffering, though it uses the output control system.

Syntax

Call output_add_rewrite_var() with the values you need. Start with this line.

PHP
$ok = output_add_rewrite_var('sid', 'abc123');
CallWhat it does
output_add_rewrite_var('id', '42')Stores id=42 for URL rewriting and starts URL-Rewriter if needed.
ob_list_handlers()Shows "URL-Rewriter" after a successful add.
output_reset_rewrite_vars()Clears rewrite vars previously added.

Which tags are rewritten depends on url_rewriter.tags (for example a=href). Absolute links to other hosts may be skipped unless the host is allowed. Once the URL-Rewriter handler is turned off, it cannot be started again in the same request.

Library declarations
PHP
output_add_rewrite_var(string $name, string $value): bool

Parameters

ParameterDescription
nameRequired. The query variable name to add to rewritten URLs or forms.
valueRequired. The value paired with that name.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Rewrite a Relative Link

Add a rewrite var, print a relative anchor, and flush so the handler rewrites the href.

PHP
<?php
ini_set('url_rewriter.tags', 'a=href');
output_add_rewrite_var('var', 'value');
echo '<a href="file.php">link</a>';
ob_end_flush();
echo PHP_EOL;

How It Works

  1. url_rewriter. tags tells PHP to rewrite a href attributes.
  2. output_add_rewrite_var() stores var=value and starts URL-Rewriter.
  3. We echo a relative link.
  4. ob_end_flush() flushes the handler, which adds ?var=value.

Example 2 — Confirm the URL-Rewriter Handler

After adding a rewrite var, list handlers to show URL-Rewriter is active.

PHP
<?php
output_add_rewrite_var('sid', 'abc');
$handlers = ob_list_handlers();
output_reset_rewrite_vars();
echo $handlers[0], PHP_EOL;

How It Works

  1. output_add_rewrite_var() starts the URL-Rewriter handler.
  2. ob_list_handlers() includes that handler name.
  3. We print the first handler name.
  4. output_reset_rewrite_vars() clears the stored rewrite variables.

Example 3 — Add More Than One Variable

Store two rewrite pairs, then flush a link so both query parameters appear.

PHP
<?php
ini_set('url_rewriter.tags', 'a=href');
output_add_rewrite_var('a', '1');
output_add_rewrite_var('b', '2');
echo '<a href="page.php">go</a>';
ob_end_flush();
echo PHP_EOL;

How It Works

  1. The first call stores a=1.
  2. The second call stores b=2 on the same rewriter.
  3. The relative href is eligible for rewriting.
  4. Flushing produces a query string with both pairs.

Frequently Asked Questions

It saves a name/value pair for URL rewriting and starts the URL-Rewriter output handler if needed so matching HTML links and forms can receive that pair.
Call output_reset_rewrite_vars(). That removes vars added by output_add_rewrite_var().
Host rules and url_rewriter settings decide which absolute URLs are rewritten. Relative URLs are the usual demo case.
It uses output buffering behind the scenes, but it is a dedicated URL-Rewriter handler, not a general-purpose capture buffer you manage like a normal ob_start() call.
No. Once that handler is turned off in a request, it cannot be started again.

Next: output_reset_rewrite_vars()

output_reset_rewrite_vars() removes all rewrite variables previously stored by output_add_.

output_reset_rewrite_vars() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful