Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess - do not rewrite URL if POST data

I have read quite many responses about htaccess but it is still so confusing to me. I have the following rule in my .htaccess file, basically removing the .php extension from the files and resolving the extensionless URL's:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.example.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.example.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

However, when I want to contact e.g. contact_form.php (that handles processing of the form data from a form in HTML), it is being rewritten to contact_form and all the POST data is lost. I would like to achieve that when the request contains post data, the URL should not be redirected/rewritten. I have honestly no idea how the rewrite rule should look like. All help greatly appreciated!

like image 393
Fygo Avatar asked Mar 12 '14 04:03

Fygo


1 Answers

You can insert this rule as first rule to ignore POST requests:

RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
like image 131
anubhava Avatar answered Sep 27 '22 23:09

anubhava