2

How can I set Apache so that it doesn't require the trailing slash on the end of the directory?

e.g. http://localhost/test will result in 404, whereas http://localhost/test/ will display the index.html file for that directory.

Update:

I should add that the example /test/ directory is in fact an alias.

1
  • 2
    After downloading WampServer and adding a new directory named test within the standard www directory, it displays the directory contents just fine with or without the trailing slash (i.e., no 404 errors). Same thing when I add an index.html inside of test. Are you possibly using a different directory than www? If so, you'll need to create an Alias Directory.
    – dan
    Commented Feb 6, 2015 at 23:39

1 Answer 1

3

I've just noticed @dan's comment above. If /test/ is indeed an Apache Alias, as set by WAMPServer, then the DirectorySlash may not be the problem after all. Both solutions are presented below.


DirectorySlash directive

You need to set DirectorySlash On in your Apache config (or .htaccess). This instructs mod_dir to implicitly append a slash in the case you mention; when the URL points to a directory.

However, this is normally On by default, so it's likely there is a DirectorySlash Off somewhere that is explicitly disabling this? So it may be preferable to find this and simply remove it.

Reference:
http://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash


Apache Alias set by WAMPServer

The solution regarding the Apache Alias (as set by WAMPServer) is contained in the Wamp Alias Directories article as linked to by @dan in the comments above.

An Apache Alias (mod_alias) allows you to serve files from a different location on your filesystem as if they are stored under the document root. So, in the case of the /test directory above, this (as it turns out) is an alias for a different location on the local filesystem.

WAMPServer provides its own interface for creating this Alias. However, in doing so it appears to append a slash to the URL-path, so /test becomes /test/. This means that you will need to also append this slash to the requested URL, otherwise it will not match!

The solution to this is to manually edit the alias file and remove this slash. From the linked article:

Click on the Wamp Server tray icon again and select Apache → Alias directories → [alias url] –> Edit alias. This will open the alias file in Notepad. Remove the trailing slash from the relative URL.

Reference:
http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias
http://willi.am/blog/2009/01/14/wamp-alias-directories/

2
  • Hi w3d, dan - sorry, I neglected to mention that it's an alias. N00b error (I've updated the question for clarification). I had a look in httpd.conf for DirectorySlash but there was no mention of it at all, anyway. Fortunately, the blog post which dan links to provides an answer - "open the alias file in Notepad. Remove the trailing slash from the relative URL... you’re alias should now work with or without the trailing slash". I can confirm that it works!
    – James
    Commented Feb 7, 2015 at 3:21
  • I've modified the answer to include the information about the Apache Alias from the linked article.
    – MrWhite
    Commented Feb 8, 2015 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.