How do sites such as Stack Overflow submit forms using action="/questions/ask/submit"
?
I would've thought mod_rewrite
would lose the $_POST
vars?
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L]
index.php
$q = explode("/", $_SERVER['REQUEST_URI']);
if($q[1]!=""){
switch($q[1]){
case "test":
include("test.php");
break;
...
test.php
<?php
if(isset($_POST["submitButton"])){
echo "Submitted";
}
else{
echo "Not submitted";
}
?>
<form method="post" action="/test/submit">
<input type="submit" name="submitButton">
</form>
If I remove action="/test/submit"
If my URL is /test
then it returns Not submitted
when I click the button.
If my URL is /test.php
then it returns Submitted
when I click the button.
For the time being, I'm using the following.
index.php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$q = explode("/", $_POST["url"]);
}
else{
$q = explode("/", $_SERVER['REQUEST_URI']);
}
...
test.php
<form method="post" action="/">
<input type="hidden" name="url" value="/test">
...
This allows my test.php to receive the post vars.
If there are no errors by the user, I use header("Location: test/success");
If there are errors, the URL will have to be /
which isn't ideal.
The problem may be with Apache 2.4. The fix was to add this line to index.php:
parse_str(file_get_contents("php://input"),$_POST);
With this method, there's no need for any action="..."
(this will successfully POST to itself, even if the URL is a slug).
mod_rewrite does not affect post variables.
action
is the target of the form. method
is the submit type. method
would be what determines if $_POST
is populated or $_GET
.
EDIT
after a down vote, and some changes to the question, it seems that this response is not what the questioner is looking for.
/questions/ask/submit
is probably controlled by a CMS. meaning most requests are redirected to a single file, which then interprets them. This redirect is done with mod_rewrite usually, as your question indicates. mod_rewrite accepts a number of 'switches' to tell it what to do. That being said, there is no known bug with mod_rewrite 'messing up' $_POST
data. Apache is what passes this data to PHP. mod_rewrite passes that to Apache. If your $_POST
data gets messed up, you have a bad rule some where.
Here is an example of how a common CMS, WordPress, directs all traffic, regardless of method, to a central controller, which then interprets the untarnished $_POST
data:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
TOP DOWN EXPLANATION:
/
index.php
then force mod_rewrite to not process more rules and load index.phpEDIT (after sample files were submitted in question)
Here are my complete files. With these exact files, on a Apache 2.2 and PHP 5.4 install, this works perfectly.
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
index.php
<?php
$q = explode("/", $_SERVER['REQUEST_URI']);
if($q[1]!=""){
switch($q[1]){
case "test":
include("test.php");
break;
}
}
test.php
<?php
if(isset($_POST["submitButton"])){
echo "Submitted";
}else{
echo "Not submitted";
}
?>
<form method="post" action="/test/submit">
<input type="submit" name="submitButton">
</form>
directory list, so you can compare permissions
NOTE: apache runs as nobody.nobody on my test machine
drwxrwxr-x 2 nobody git 4096 Dec 22 03:34 ./
drwxrwxr-x 3 nobody git 4096 Dec 22 03:26 ../
-rw-rw-r-- 1 nobody git 244 Dec 22 03:29 .htaccess
-rw-rw-r-- 1 nobody git 134 Dec 22 03:27 index.php
-rw-rw-r-- 1 nobody git 194 Dec 22 03:28 test.php
If this does not work for you, you probably have a permission problem. This exact code works.