Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP method="post" stopped working after I added this .htaccess... Why?

I've added this .htaccess to remove file extension from the URL, so instead "index.php", it would show "index" only, all the times. But after I did it, my <form method="post"> stopped working

Options +FollowSymLinks -MultiViews
Options +Indexes
AcceptPathInfo Off
RewriteEngine on
RewriteBase   /

#Force from http to https
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^mysite.com$
RewriteRule ^(.*)$ https://mysite.com/$1 [R=301]

#take off index.html
 RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]    

## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]  

Here's an example:

/* Worked before .htaccess, but not anymore */

    <form method="post" action="pwreset.php"  class="form-stacked">

/* Removing .php from action works. But there are hundreds of files and this method is not very trustworthy */

    <form method="post" action="pwreset"  class="form-stacked">

PS: If I use regular .htaccess rules, like this one:

RewriteRule ^index$ ./index.php [L,NC]

It won't hide .php in all cases

like image 926
Lucas Bustamante Avatar asked Nov 05 '13 18:11

Lucas Bustamante


Video Answer


2 Answers

This is happening because you are redirecting here:

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

When you redirect, the request BODY doesn't always get included, you can try adding an exception for POST:

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
like image 168
Jon Lin Avatar answered Sep 28 '22 07:09

Jon Lin


Because the action of your form is pointing to /pwreset.php. When you try to go to that page (even via a form post), the htaccess will redirect you to /pwreset before any PHP code runs. A redirect will drop any POST data from the new request.

You're going to have to change all those form actions to have the non-php version. As a short term fix, try excluding POST requests from your redirect rule

RewriteCond %{REQUEST_METHOD} !POST

like image 35
Adam Nicholson Avatar answered Sep 28 '22 07:09

Adam Nicholson