Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Rewrite url and preserve posted data

I have a php server and i want to rewrite incoming urls. Since incoming requests are post requests I also want posted data to be transferred at the new urls.

I have managed to redirect urls with the following rewrite rules:

RewriteRule ^test/(.*)$ http: //localhost/index.php?data=&1 [NC,L]

or

RewriteRule ^test/(.*)$ http: //localhost/index.php?data=&1 [NC,R=301]

I have also managed to preserve post data with the following rewrite rule:

RewriteRule ^test/(.*)$ http: //localhost/index.php?data=&1 [P]

The problem is that I can not both of them at the same time. What am I doing wrong? Is there a way to redirect url and keep post data?

like image 996
reven Avatar asked Sep 22 '11 07:09

reven


1 Answers

Your issue probably is that you do redirects instead of rewrites. Redirects change the URL people see in the browser. Rewrites only change the URL that your PHP app sees. The latter is normally what you want. And in the latter case POST variables are automatically preserved.

In order to rewrite instead of redirect, use a local path instead of a URL:

RewriteRule ^test/(.)$ index.php?data=$1 [NC,L]
like image 155
NikiC Avatar answered Nov 02 '22 01:11

NikiC