0

I have an UBUNTU server with Apache. POST request to specific endpoint on this server is automatically redirected by Apache to a GET request if the URL does not have a trailing slash

On that path, the server has a index.php file that basically gets the body and inserts into a mySQL dB.

https://www.myserver.com/py/order/ORDER-0001 --> Does not work

Apache access.log:

186.139.yy.xx - - [04/May/2023:07:42:18 -0300] "POST /py/order/ORDER-0001 HTTP/1.1" **301** 5380 "-" "PostmanRuntime/7.32.2"
186.139.yy.xx - - [04/May/2023:07:42:18 -0300] "**GET** /py/order/ORDER-0001/ HTTP/1.1" 200 236 "https://www.myserver.com/py/order/ORDER-0001" "PostmanRuntime/7.32.2"

However, https://www.myserver.com/py/order/ORDER-0001 --> Works perfectly fine

Apache access.log:

186.139.yy.xx - - [04/May/2023:07:49:28 -0300] "POST /py/order/ORDER-0001/ HTTP/1.1" 200 4964 "-" "PostmanRuntime/7.32.2"

I have tried many things as mentioned on these but still nothing works for me:

The latest I've tried is "forcing" a trailing slash with a 307 redirect. I have added this snippet on apache2.conf

<Directory /var/www/html/py/order/ORDER-0001>
   AllowOverride All
   Require all granted
   Options +FollowSymlinks

   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*[^/])$ /$1/ [L,R=307]
</Directory>

Can anyone please help on this?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
mariansdj
  • 23
  • 6
  • Why do you need to allow this in the first place? POST requests should really be going directly to the file that handles the request (index.php), not the “directory”. Filesystem directories don’t accept requests themselves, you are relying on mod_dir issuing an internal subrequest for the DirectoryIndex, which you’ve not explicitly defined here. – MrWhite May 04 '23 at 11:39
  • Thanks for your comment MrWhite. I have an external provider who asked for the endpoint. The explicitly requested an URL without "index.php". – mariansdj May 04 '23 at 14:34
  • May be you simply have to rewrite to index.php "internally", ala wordpress style? `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]` or similar? If not, maybe the !-d line could solve the trick – Daniele Continenza May 04 '23 at 15:05
  • @mariansdj Then give them the URL _with_ the trailing slash? (URLs with and without the trailing slash are two different URLs.) If you request a filesystem directory without the trailing slash then mod_dir "fixes" the URL by 301 redirecting to append trailing slash (yes, this converts the POST request to GET and the POST data is lost). – MrWhite May 04 '23 at 15:19
  • @DanieleContinenza That would not seem to be the problem. The "problem" would seem to be that the OP has given the wrong endpoint to the external provider? – MrWhite May 04 '23 at 15:20
  • @mariansdj The two URLs you've given ("Does not work" and "Works perfectly fine") are exactly the same. The later should presumably have a trailing slash. – MrWhite May 04 '23 at 15:24
  • @MrWhite sorry I notice right now the rewrites are inside a Directory statement! – Daniele Continenza May 04 '23 at 15:28
  • @YourCommonSense The "dupe" does not help this particular case. Here the OP is attempting to use a filesystem directory without a trailing slash as the endpoint for their API. However, Apache itself triggers a 301 redirect (so converting the POST to a GET). They need to first override Apache's default behaviour - which they are currently failing to do. – MrWhite May 04 '23 at 15:28
  • @DanieleContinenza Yes, attempting to "redirect" inside a `` container for the directory itself is not going to work. However, you should not be redirecting at all here. You basically need to follow the same principles as I've outline in [my answer](https://stackoverflow.com/a/76168997/369434) to the following question: https://stackoverflow.com/questions/76166738/htaccess-how-to-return-404-instead-of-301-for-directories-with-missing-ending – MrWhite May 04 '23 at 15:33
  • Hello both, thank you very much for your time. I have been struggling with this and still can't make it work. Seems I am still doing something wrong. The lastest I've done is to create a .htaccess file (and leave apache2.conf as it was) URL: https://www.server.com/py --> Still doing a 301 and adding the trailing slash works perfectly fine – mariansdj May 06 '23 at 18:53
  • Can you show the logs where there's actually a 307 used? – covener May 06 '23 at 20:42
  • Sure, thanks covener! `181.229.xx.xx - - [06/May/2023:16:55:26 -0300] "POST /py/order/ORDER-0001 HTTP/1.1" 301 913 "-" "PostmanRuntime/7.32.2"` Then: `181.229.xx.xx - - [06/May/2023:16:55:26 -0300] "GET /py/order/ORDER-0001/ HTTP/1.1" 307 641 "https://www.myserver.com/py/order/ORDER-0001" "PostmanRuntime/7.32.2"` – mariansdj May 07 '23 at 13:27
  • For anyone who gets to this post and has the same issue I had. After hours and hours of trying, the solution was: 1- Get in apache2.conf (or apache config file) and add a rule poiting to your directory: AllowOverride All 2- Go to your directory /var/www/html/py and create an .htaccess file 3- Add these lines (note I am doing a 307 redirect from the entire address and created a new destination folder with last /) RewriteEngine On DirectorySlash Off RedirectMatch 307 /py/order/ORDER-0001 /ya/ This made it work perfectly. – mariansdj May 08 '23 at 17:00

0 Answers0