12

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 was (RewriteRule ^index.php / [L,R=301] in the .htaccess file). And it works!

Since I put that string into the .htaccess, some pages are redirected to the main page. I spent a lot of time to guess, why. As I understand, the answer is: with RewriteRule ^index.php / [L,R=301], $_POST parameters are not sent to the next page. $_GET parameters are OK. Once I remove RewriteRule ^index.php / [L,R=301] from .htaccess, everything becomes fine as usual. Why does it happen and how to fix that?

Thank you.

Community
  • 1
  • 1
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • 1
    got the same problem with only [L] flag. This occured when changing hosting so i guess this is, somehow, config related. – tsadiq Feb 22 '12 at 14:19
  • Here's a good link about [P] flag :) http://stackoverflow.com/questions/358263/htaccess-is-it-possible-to-redirect-post-data – tsadiq Feb 22 '12 at 14:23

6 Answers6

20

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]
mario
  • 144,265
  • 20
  • 237
  • 291
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.

Gerben
  • 16,747
  • 6
  • 37
  • 56
6

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?

Community
  • 1
  • 1
Johny
  • 1,441
  • 10
  • 26
  • This seems like a replacement to apache's existing 404 handling. More to the point, this doesn't seem to address the question. – Umbrella Jan 11 '12 at 21:52
4

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.)

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • 1
    The [R=301] will break POST requests (form data lost), but leaving it out will not affect the address bar. For this reason, you would want to avoid doing this kind of rewrite if (ever) POSTing to index. – Umbrella Jan 11 '12 at 21:49
  • 2
    Yep. One could always add a `RewriteCond %{REQUEST_METHOD} !POST` before the rewrite, but is hesitate to think _why any form would still have index.php as an action_ ... – Wrikken Jan 11 '12 at 22:09
1

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]
Adrian
  • 11
  • 2
0

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

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

Kodali444
  • 1,283
  • 1
  • 17
  • 21