0

I am trying to build 'Clean' urls for my REST API.

In /test/index.php I have the following code:

<?php

var_dump($_POST);

I am using Postman to POST data here:

http://localhost:8888/test/index.php

It returns the following as expected:

array(1) {
  ["test"]=>
  string(14) "1"
}

When I use Postman without index.php:

http://localhost:8888/test

array(0) {
}

The data is dropped, even though it is hitting the index.php.

Now what is really strange is if I Post to:

http://localhost:8888/test/

array(1) {
  ["test"]=>
  string(14) "1"
}

It works. I do not want the trailing slash.

Now to fix this I added this to my .htaccess:

RewriteRule test test/index.php [NC,P]

As has been suggested in this article:

Is it possible to redirect post data?

That still isn't working. What am I missing?

CloudJake
  • 156
  • 4
  • 22

1 Answers1

0

To hide index.php from the URL you need to exploit mod_rewrite. Put this in your root .htaccess file, but you can also do that in your apache configuration file (e.g, 000-default).

mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6. (sudo a2enmod rewrite)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

No other way to edit the dispatchers without rewriting the url, unless you use a custom server.

donnadulcinea
  • 1,854
  • 2
  • 25
  • 37
  • Hi Donna, as I mentioned in the post down the bottom I can't use .htaccess as this only works on apache servers and I need it to work across all kinds of server (like nginx.) I need a universal fix and also I don't have control of the server so can't make any changes to the apache configuration. Do you know anyway I can achieve this without using apache settings? – CloudJake Sep 28 '18 at 02:32
  • As I say in my answer, If you don't want to use .htaccess, you could edit the apache conf file, or maybe if you're using virtual hosts edit the site's conf file. You're going to need to do something so the WebServer knows what to do. If you are using a nginx server, then you can do it without .htaccess, but then you need to configure the config file for nginx. Or, as I stated, you need to rely on another web server. – donnadulcinea Sep 28 '18 at 02:38
  • Thanks Donna, very frustrating as so many companies do it you would expect a better solution by now with forwarding POST data. I have test using the .htaccess file the problem is it works when I post to http://localhost:8888/test/ but if I remove the end slash it does not http://localhost:8888/test. A requirement for this project is to not have the slash at the end. Any ideas? – CloudJake Sep 28 '18 at 02:47
  • Check this answer: https://stackoverflow.com/questions/12952225/remove-index-php-and-trailing-slashes-from-url – donnadulcinea Sep 30 '18 at 16:41