Note that there are some explanatory texts on larger screens.

plurals
  1. PODo variables sent using POST get sent to PHP if using mod_rewrite?
    text
    copied!<p>How do sites such as Stack Overflow submit forms using <code>action="/questions/ask/submit"</code>?</p> <p>I would've thought <code>mod_rewrite</code> would lose the <code>$_POST</code> vars?</p> <p><b>.htaccess</b></p> <pre><code>RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L] </code></pre> <p><b>index.php</b></p> <pre><code>$q = explode("/", $_SERVER['REQUEST_URI']); if($q[1]!=""){ switch($q[1]){ case "test": include("test.php"); break; ... </code></pre> <p><b>test.php</b></p> <pre><code>&lt;?php if(isset($_POST["submitButton"])){ echo "Submitted"; } else{ echo "Not submitted"; } ?&gt; &lt;form method="post" action="/test/submit"&gt; &lt;input type="submit" name="submitButton"&gt; &lt;/form&gt; </code></pre> <p><b>If I remove</b> <code>action="/test/submit"</code></p> <p>If my URL is <code>/test</code> then it returns <code>Not submitted</code> when I click the button.<br> If my URL is <code>/test.php</code> then it returns <code>Submitted</code> when I click the button.</p> <hr> <h1>Update</h1> <p>For the time being, I'm using the following.</p> <p><b>index.php</b></p> <pre><code>if($_SERVER['REQUEST_METHOD'] == 'POST'){ $q = explode("/", $_POST["url"]); } else{ $q = explode("/", $_SERVER['REQUEST_URI']); } ... </code></pre> <p><b>test.php</b></p> <pre><code>&lt;form method="post" action="/"&gt; &lt;input type="hidden" name="url" value="/test"&gt; ... </code></pre> <p>This allows my test.php to receive the post vars.</p> <p>If there are no errors by the user, I use <code>header("Location: test/success");</code></p> <p>If there are errors, the URL will have to be <code>/</code> which isn't ideal.</p> <h1>Update/resolution</h1> <p>The problem may be with Apache 2.4. The fix was to add this line to index.php:</p> <pre><code>parse_str(file_get_contents("php://input"),$_POST); </code></pre> <p>With this method, there's no need for any <code>action="..."</code> (this will successfully POST to itself, even if the URL is a slug).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload