Is it possible to redirect post data?

Is it possible to redirect post data?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Is it possible to redirect post data?

You are facing a classic conflict between URL rewriting and request method preservation. Your current setup uses Apache's .htaccess to route all non-existent files to index.php, relying on PHP to parse the REQUEST_URI. While this works perfectly for GET requests where you simply check isset($_GET['send-mail']), it breaks your POST data because the RewriteRule is currently appending a query string argument instead of preserving the original request method and body.

When your form submits to /send-mail, the rewrite rule transforms the URL into index.php?send-mail. However, Apache does not automatically change the HTTP method from POST to GET during this rewrite. The browser sends a POST request, but because the target is now treated as a script with query parameters, the data often gets lost or the logic fails if your controller expects the URI segment rather than the query string.

To solve this without dropping your POST payload, you need to ensure the rewrite rule preserves the original HTTP method and that index.php reads from the correct superglobals. The most robust solution involves modifying your .htaccess configuration to explicitly pass the original request information to the frontend controller.

Update your .htaccess rules to look like this:

RewriteEngine On

# Serve files and dirs if they exist, otherwise send to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L,QSA]

# Specific rule for the mail form
# This captures the path and passes it as a query parameter only if needed,
# but allows the original method to pass through.
RewriteRule ^send-mail$ /index.php [L,QSA]

The critical addition here is the QSA (Query String Append) flag, which ensures that if your form accidentally had existing query parameters, they are preserved. However, the real fix lies in how index.php handles the incoming request. You must check for POST data using $_POST regardless of whether the route was triggered by a URI segment or a query parameter.

In your index.php, ensure you are not relying solely on $_GET. Instead, implement a logic flow that checks both sources:

<?php
// Determine the intended action from either GET or POST
$action = $_GET['send-mail'] ?? $_POST['send-mail'] ?? null;

if ($action === 'send-mail') {
    // Process the form data found in $_POST
    $email = $_POST['email'] ?? '';
    $message = $_POST['message'] ?? '';
    
    // Your logic to handle the mail submission goes here
    processMail($email, $message);
} else {
    // Render the default content based on REQUEST_URI
    renderDefault();
}

By using the null coalescing operator (??), you create a unified entry point. If the browser sends a POST to /send-mail, the $_POST['email'] array will contain the data, and your script can process it immediately. The URL rewriting happens transparently in the background, allowing the user to see /send-mail in their address bar while the server executes PHP logic that accesses the POST body.

This approach avoids the pitfalls of converting everything to GET requests, which would strip sensitive or large payloads from $_POST. It also maintains your clean routing architecture where index.php acts as a single entry point for all dynamic content. If you are moving toward a framework-based architecture later on, this pattern mirrors how modern MVC frameworks handle route mapping and request resolution, ensuring that your custom logic remains decoupled from the underlying HTTP method mechanics.

Our network