on trailing slash in url

If you type “http://domainhostseotool.com” in your browser’s address bar, the request uri is /(_SERVER["REQUEST_URI"]), i.e, you are actually visiting http://domainhostseotool.com/. This is nothing to do with the Apache server, the browser sends the following request:

If you type “http://domainhostseotool.com/”(note that the slash at the end), your browser will send the exact same request to the server. Similarly, the browser sends the same request to the server for “http://domainhostseotool.com?q=v” and “http://domainhostseotoo.com/?q=v”:

and _SERVER["REQUEST_URI"] is /?q=v

But things get complicated when you visit “http://domainhostseotool.com/dir”, supposing dir is a folder in the document root. The request sent by the browser to the server is:

while the response is a 301 redirection which redirects to the corresponding url with an end slash:

The browser then accesses the redirected url:

If you visit the slash-ending url directly, the response is 200ok instead of 301 redirection. Similarly, the url without trailing slash and with a query string(http://domainhostseotoo.com/dir?q=v) would produce the following messages:

while url with trailing slash(http://domainhostseotool.com/dir/?q=v) won’t produce the 301 redirection.

This is the default behavior of Apache which redirects url whose path has no ending slash to url with ending slash appended to its path, if the url corresponds to a directory. You can use Apache configuration parameter DirectorySlash to change that behavior and turn the non-ending slash to ending slash redirection off:

Now what will happen when you visit http://domainhostseotoo.com/dir? On our system, it produces the following message:

Forbidden

You don’t have permission to access /dir on this server.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

This is because with DirectorySlash turned off, apache won’t bother to add a trailing slash and find index files in that directory(even the index file does exist in that directory)(“DirectoryIndex will be evaluated only for directories requested with trailing slash.“). Apache will try to list the content of that directory but in our case, directory listing is disabled(Options -Indexes, and forbidding listing directories is the default behavior of apache 2.4), so the above messages is produced. See also this post.

 

Posted in tips of hosting