.htaccess URL重写后表单不起作用

时间:2015-04-20 13:02:32

标签: php forms apache .htaccess mod-rewrite

我从其他一些帖子中复制了以下代码。它成功地帮助我从URL中剥离.php扩展名。但是,在实现之后,我的一些表单不再有效。我的表单类似于

<form method='post' action='/users/activate/Qwerty'></form>

现在,我提交表单时没有将数据发送到该位置。我意识到如果我将action='/users/activate/Qwerty'更改为action='?action=activate&code=Qwerty123',表单将再次有效。但是有很多页面包含发布到我网站中不同位置的表单,我不想手动更改这么多页面。所以我想知道为什么数据没有按预期发布,我该如何解决这个问题呢?

我的.htaccess低于

#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>

1 个答案:

答案 0 :(得分:1)

如果您在提交时丢失了POST数据,则表示您获得了重定向。

现在应该会更好

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>