I’m posting a request with POST data using cURL to a PHP script here: http://localhost/example/index.php
How do I hide index.php and just post to http://localhost/example ?
I’ve tried just posting there, but I get a 301 Moved Permanently header, and if I set CURLOPT_FOLLOWLOCATION to TRUE, the request follows, but seems to lose the POST data – I get nothing at the far end using this file_get_contents(‘php://input’).
POST data is lost upon redirect and you can’t avoid that.
What you can do to achieve what you need is to specify in .htaccess (assuming you run Apache)
And then by requesting
http://localhost/example/you will recieve index.php in that directory.The problem appears if you want to give the index.php also for requests going to
http://localhost/example(without trailing slash), since apache redirects to the url with trailing slash by default, loosing POST data in process. To deal with it, you can fake the request dir:The
/?points Apache to use the rule for both cases: with and without trailing slashes. To avoid redirect, you need to skip theR=301flag making it[L]. With redirect it would be[R=301,L].