Seems like POST values are lost when .htaccess RewriteRule used. GET values are OK. How to fix?

前端 未结 6 1888
野趣味
野趣味 2020-12-01 18:54

Several days ago I had a question about removing index.php from the address bar, so the address of the page looks shorter and better. The shortest solution of this problem w

相关标签:
6条回答
  • 2020-12-01 19:07

    POST values will NEVER survive an external redirect (the R=301), it's a security liability, so browsers will never support that. Remove the R=301 and you will be fine. You just should alter all existing links to the page to the shorter/prettier one (<a>'s but also form actions etc.)

    0 讨论(0)
  • 2020-12-01 19:09

    In My case I used .htaccess. Refer : PHP $_POST not working?

    i.e action="booking.php" to action="booking" worked for me

    0 讨论(0)
  • 2020-12-01 19:12

    I'm using something like:

    <IfModule mod_rewrite.c>
    
    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} !^/(css|images|js)/
    
    # don't rewrite existing files, directories and links
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    
    
    # rewrite everything else to index.php
    
    RewriteRule .* index.php [L]
    
    </IfModule>
    

    And its working for all requests, rewriting it via index.php file. If you need to redirect 301 (which stands for Moved Permanently code) check out this question: Is it possible to redirect post data?

    0 讨论(0)
  • 2020-12-01 19:16

    You could try using [L,R=307] instead. 307's must not change the request-method according to the spec, but I don't know how browser implemented 307.

    But the root of the problem is the use of <form action="____/index.php" ...

    Just leave the action empty to POST to the current url e.g.

    0 讨论(0)
  • 2020-12-01 19:29

    The [R] flag will incur a redirect. And user-agents issue a redirect as GET request. There is nothing that can be done if you really want to shorten URLs down to the / root path.

    You could however block POST requests specifically from being rewritten/redirected:

    RewriteCond %{REQUEST_METHOD} !POST
    RewriteRule ^index.php / [L,R=301]
    
    0 讨论(0)
  • 2020-12-01 19:32

    I had the same problems but my htacces was like this:

    RewriteEngine on
    RewriteRule .* index.php [NC]
    

    Just change NC for L and everything works fine.

    Final code:

    RewriteEngine on
    RewriteRule .* index.php [L]
    
    0 讨论(0)
提交回复
热议问题