Reputation: 1511
I copied the below code from some other posts. It successfully helped me to strip the .php extension from the URL. However, after implementing this, some of my forms are no longer working. I have forms that are posting like
<form method='post' action='/users/activate/Qwerty'></form>
Now it is not sending the data to the location when I submit the form. I realize that if I change the action='/users/activate/Qwerty'
to action='?action=activate&code=Qwerty123'
, 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?
My .htaccess is below
#the line below will hide folders and their contents on any apache server. Use 'Options +Indexes' to unhide
Options -Indexes
#the line below will hide folders and their contents on goDaddy. Use 'Options +MultiViews' to unhide
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#maintain post data after rewrite
RewriteCond %{REQUEST_METHOD} =POST
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
#I want to rewrite the url http://127.0.0.1/users/activate/Qwerty123/
#so that I can get values with php like so $action = $_GET['action']; or $code=$_GET['id'];var_dump($action);//outputs visa var_dump($code);//outputs 203
#and also when the user makes a mistake and types http://127.0.0.1/users/activate/ it takes them to http://127.0.0.1/users.php
RewriteCond %{REQUEST_URI} ^/users/([A-Za-z0-9-]+)/?$
RewriteRule ^users/(.*)/?$ /users.php?action=$1 [QSA,NC,L]
RewriteRule ^users/([A-Za-z0-9-]+)/([A-Za-z0-9-,=]+)/?$ /users.php?action=$1&code=$2 [QSA,NC,L]
#the lines below relocate all 404 to the index page
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)?$ /index.php [NC,L]
</IfModule>
Upvotes: 1
Views: 1292
Reputation: 19026
If you lost POST
data when submitting, it means you get a redirect.
This should be better now
Options -Indexes -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# force WWW for remote servers (not localhost)
RewriteCond %{HTTP_HOST} !^(?:www\.|localhost$) [NC]
RewriteCond %{HTTPS}s ^on(s)$ [NC]
RewriteRule ^(.*)$ http%1://www.%{HTTP_HOST}/$1 [R=301,L]
# remove php extensions (only for GET method)
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1/? [L,R=301]
# don't touch other existing files/folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# force trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1/ [L,R=301]
# rewrite extensionless php files
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/$ $1.php [L]
# users activate
RewriteRule ^users/([^/]+)/?$ users.php?action=$1 [NC,L]
RewriteRule ^users/([^/]+)/([^/]+)/?$ users.php?action=$1&code=$2 [NC,L]
# finally, if not found
RewriteRule ^ index.php [L]
</IfModule>
Upvotes: 1