Reputation: 560
I am hiding the php file extention using the below directives in Apache htaccess file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I have a simple html form like this
<form action="register.php" method="post">
<input type="text" name="name">
<input type="submit">
</form>
And in my PHP page this is how I was earlier checking whether the form has been posted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* code to process the post */
}
But since I added the htaccess directives to hide the php filename extensionthe "POST" becomes "GET" and the above code never executes. Is there a good way to handle this? I know it is the internal redirect that causes this but is there an elegant way to handle this? I don't want to use the below method for obvious reasons
if (isset($_POST['submit'])) {
Upvotes: 3
Views: 2869
Reputation: 1060
Maybe someone has the problem with ajax call.
In this case I had a rewrite rule with [L,R]. It must be only [L] otherwise "POST becomes GET".
Upvotes: 0
Reputation: 785286
POST
doesn't become GET
, it is due to your first redirect rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
Which is catching a request with .php
in it and redirecting to a URL without .php
in it. You're looking at $_SERVER["REQUEST_METHOD"]
after it has been redirected by this rule.
To fix have it this way:
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
i.e. don't redirect POST
requests to avoid POST
data getting lost.
Upvotes: 3