Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 545
Haradzieniec Avatar asked Jan 11 '12 21:01

Haradzieniec


People also ask

How do I know if htaccess rewrite is working?

Save the file and type the URL yoursite.com/foobar/ . If the reditect works and the URL gets redireted to the homepage of example.com then it's clear that your htaccess is working and being read by your Apache server. If it still doesn't work then the problem might be that your hosting provider has not enabled it.

What is htaccess RewriteRule?

htaccess rewrite rule includes setting a combination of rewrite condition ( RewriteCond ) tests along with a corresponding rule ( RewriteRule ) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine on line in the . htaccess file located in the website's docroot.

How long does it take for htaccess to change effect?

htaccess files follow the same syntax as the main configuration files. Since . htaccess files are read on every request, changes made in these files take immediate effect.

Does order matter in htaccess?

It does matter. Quoting from the documentation for RewriteRule: The order in which these rules are defined is important - this is the order in which they will be applied at run-time.


2 Answers

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.

like image 109
Gerben Avatar answered Sep 26 '22 06:09

Gerben


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?

like image 39
Johny Avatar answered Sep 24 '22 06:09

Johny