Trailing slashes

ScotWatson

New Member
Messages
13
Reaction score
0
Points
1
I am working on an API and I have come across the issue of trailing slashes. So far, I have been storing the php code for each endpoint into a file called index.php in a folder with the name of the endpoint. When I try with the trailing slash, I get the response generated by the php code. When I try without the trailing slash, I get a 301 (Permanently Moved) redirect, then the request and response I get with the trailing slash. I did some research and found that this behavior can be obtained with certain commands in the .htaccess file, but I do not have any of those commands in that file. Is it standard for servers to behave this way? Is it possible to change this behavior?
Web searches seem to reveal that although there is no official standard, it seems like there is a de facto standard to not use trailing slashes in APIs. Even if the behavior is turned off, in order to not have a trailing slash, I would need a file with the name of the endpoint and no file extension, but then it would not have a php extension, so the code would never get interpreted. So I am not sure how to support not having a trailing slash without an intermediate redirect.
Sorry for the long post, but these questions seem too closely related to make them separate.
 

fusio

Member
Messages
51
Reaction score
4
Points
8
.htaccess files help configure routes and so on...
Can you put the code of what you are explaining?
 

ScotWatson

New Member
Messages
13
Reaction score
0
Points
1
I have just added a file to illustrate the issue. It is called index.php and is stored in a folder called test under the public_html folder. The file contains the following code:
PHP:
<?php
// Handles https://www.scotwatson.x10.mx/test/
echo "Testing...";
?>
To see the issue, open the browser, open DevTools, click on Network tab, select "Disable cache". If "https://www.scotwatson.x10.mx/test/" is entered into the address line, the result is:
Screenshot 2024-12-28 111326.png
If "https://www.scotwatson.x10.mx/test" is entered into the address line, the result is:
Screenshot 2024-12-28 111352.png
This shows that "https://www.scotwatson.x10.mx/test" uses an intermediate 301 redirect response to provide the same result as "https://www.scotwatson.x10.mx/test/". This is the behavior I referenced above. Is this typical? Is there a setting change this? If so, how do I control the responses to "https://www.scotwatson.x10.mx/test" vs "https://www.scotwatson.x10.mx/test/"?
 

ScotWatson

New Member
Messages
13
Reaction score
0
Points
1
Also, my .htaccess file, which is at the public_html folder, contains the following:
Code:
Options -Indexes
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "*, Authorization"
 
Top