4.6
(8)

Last Updated on February 9, 2022 by Amit

If you are using htaccess to rewrite URLs or to remove file extension ,then you might sometimes face this issue as your HTML form stops sending POST data.
The problem occurs because the rule you are using to remove file extension from URLs does two types of URL redirections.
The first redirection is when your rule redirects file.ext to file externally. The second redirection happens internally as the file without extension has to be mapped to its original location.
Your HTML post data gets lost when your RewriteRule makes an external redirection of URLs for example when you press the submit button on your form , the URL changes from its original format /destination.php to /destination (The Change in URL is done by the RewriteRule) .
To avoid this, and to fix this problem I am posting a working solution in article. If you are having this problem on your server then don’t worry, it’s not something that is hard to fix . You can fix it easily by adding a single line to your htaccess. With just a minor modification you can get your HTML form back to normal.

Fix HTML form POST data lost with htaccess

With htaccess, it’s easy to identify wether the request was made by GET or POST. You can use %{REQUEST_METHOD} variable to check it. The value of this variable is GET by default unless a request was from HTML form using method=”post” .

The quickest and most effective way to fix your post data by htaccess is to put the following line in your htaccess file.

RewriteCond %{REQUEST_METHOD} ^POST$ [NC]
RewriteRule ^ - [L]

Just put the code above at the top of your htaccess and below RewriteEngine On line.

This tells Apache rewrite server to skip rewriting for all files sending the request method as POST. Or in simple words, it tells mod rewrite not to rewrite form destination file .

If you are using the following rule in your htaccess file to remove .PHP extension :

RewriteEngine on
#1) redirect /file.php to /file
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule (.+) /%1 [L,R=301]
#2) map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)/?$ /$1.php [L]

Then you will just need to put a condition check above the redirect rule , updated the first Redirect part with this

#1) redirect /file.php to /file
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
#exclude post requests
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteRule (.+) /%1 [L,R=301]

You can try any of the method I mentioned above, however the first method is quite simple. You can easily put a rule with condition at the top of your htaccess file to fix this issue.
I hope this article was helpful . If you are interested in learning about htaccess , I have written a short htaccess tutorial for beginners, you can follow this tutorial to learn about htaccess and URL rewriting.
If you have any questions about this article or anything related to htaccess, just post a comment below.

How useful was this post?

Click on a star to rate it!

Average rating 4.6 / 5. Vote count: 8

No votes so far! Be the first to rate this post.