Home » Php » php – Why the trailing slash in the web service is so important?

php – Why the trailing slash in the web service is so important?

Posted by: admin July 12, 2020 Leave a comment


I was testing a web service in PHP and Python. The address of the web service was, let’s say, http://my.domain.com/my/webservice. When I tested the web service in PHP using that URL everything worked fine. But, when I used the same location but in Python using SOAPpy I got an error.

Below is the code I used to communicate with the web service (Python):

from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice', namespace)

The respond I got from the server:

HTTPError: <HTTPError 301 Moved Permanently>

I figure out that if I add a trailing slash to the web service location it works!

from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice/', namespace)

Why the lack of the trailing slash causes the error?

How to&Answers:

They’re different URLs. http://my.domain.com/my/webservice implies a file webservice in the my folder. http://my.domain.com/my/webservice/ implies the default document inside the my/webservice folder.

Many webservers will automatically correct such URLs, but it is not required for them to do so.


Because the actual server URL is:

The PHP library must be following redirects by default.


The error is a 301 redirect meaning the you are being redirected to the URL with the slash on the end by the web server.

It seems that PHP will auto follow this redirect and thus not throw the error, whereas Python won’t. You will need to do the following:

  1. Try to Connect to the initial URL
  2. Catch any 301 redirect and possibly 302 redirects as well
  3. If there was a redirect then try to connect to that URL instead.

The new URL should be available in the response headers.



[Disclaimer: This is a copy of my answer from here. I know some people don’t like this kind of copying, but this explains why the slash is important.]

Imagine you serve a page


that contains

<a href="more.html">Read more...</a>

On click, the user’s browser would retrieve http://mydomain.com/more.html. Had you instead served


(with the same content), the browser would retrieve http://mydomain.com/bla/more.html. To avoid this ambiguity, the redirection appends a slash if the URL points to a directory.


What a SOAP-URL looks like is up to the server, if a slash is necessary depends on the server and the SOAP implementation.

In your case, I assume that the target server is an apache server and the SOAP URL is actually a directory that contains your SOAP handling script.
When you access http://my.domain.com/my/webservice on the server, apache decides that the directory is properly addressed as http://my.domain.com/my/webservice/ and sends a 301 redirect.

SOAP uses a http POST, its up to the client to decide if the redirect should be followed or not, I assume that it just doesn’t expect one.

Other implementations of SOAP, e.g. Apache Axis in Java have URLs that look like Servlets, e.g. http://domain.com/soap/webservice without slash, in this case the URL without slash is correct, there is no directory that exists anyway.

Axis fails on redirects as well, I think.