user2335065
user2335065

Reputation: 2567

Why are my forms not working after .htaccess URL rewrite?

I copied the below code from some other posts. It successfully helped me to stripped the .php extension from the URL. However, after implementing this, my forms are no longer working. I have forms that are posting like <form method='post' action='login.php' .... Now it is not sending the data to the location when I submit the form. I realize that if I change the action='login.php' to action='login', the forms will work again. But there are a number of pages that contain forms posting to different locations in my website, and I do not want to change so many of them manually. So I wouold like to know why the data are not posting as expected and how can I solve the problem. Thank you!

RewriteEngine On

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

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

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

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

Upvotes: 2

Views: 2033

Answers (2)

Kamal Kumar
Kamal Kumar

Reputation: 3763

I want to redirect user_login.php to seo friendly url like /user-login with post form data and this worked for me.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/user_login\.php [NC]
RewriteRule ^ user-login [QSA,R=301]
RewriteRule ^user-login$ user_login.php [QSA,L]

In view file

<form action="<?php $siteurl;?>/user-login" method="post" id="user_login">

Upvotes: 0

anubhava
anubhava

Reputation: 786349

Add this rule on top before other rules to skip all POST requests from rewrite:

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

Upvotes: 3

Related Questions