0

I just want to remove a trailing slash from a directory. For example I want /p/page/ to show up as /p/page. It just looks better, doesn't it?

However I've tried many different kinds of mod_rewrites but none have worked or something happened.

I just want this to apply to subfolders (even better, any slash in a folder in a folder in a folder like /a/b/c), not /p/ as this may affect other parts of my site in a negative way.

SkyPilot
  • 13
  • 4

2 Answers2

0

You can try adding the following line to your .htaccess file:

DirectorySlash Off

That solved the problem for me a while ago. Of course if the path is only / I don't think you can get rid of it.

animuson
  • 53,861
  • 28
  • 137
  • 147
  • That's odd, are you sure you typed it correctly? Maybe check the Apache error log to see what it said? – animuson Sep 02 '11 at 04:45
  • I'm sure. I just copied and pasted your exact comment. I then decided to include Rewrite Engine On but still got it. Or was I supposed to do something else? – SkyPilot Sep 02 '11 at 04:50
  • You shouldn't need to include any other options to make that work. This command is part of mod_dir, not mod_rewrite. You could check if Apache has mod_dir enabled which would be quite surprising if it didn't. – animuson Sep 02 '11 at 04:52
  • Well, my host's cPanel states DirectorySlash was not found, perhaps either by a server misconfiguration or a module not included. Isn't there a way by using mod_rewrite? – SkyPilot Sep 02 '11 at 04:54
  • No, without that option Apache will always add a slash to the end of the URL as it believes it is a directory. One sort of workaround would be tricking Apache by creating a file named 'page' (no extension) at that location. This should make Apache think that the path leads to a file instead of a directory and thus not append the slash. This would require you to do so for *every* page on your website though and I haven't tested it so I don't even know if it would actually work. Just a thought. – animuson Sep 02 '11 at 14:43
0

Copy this code in your root .htaccess file (directly under DOCUMENT_ROOT):

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L]

DirectorySlash Off
RewriteCond %{THE_REQUEST} ^GET\s(.*)/\s
RewriteRule ^ %1 [R=302,NE,L]

It will externally redirect http://localhost/blog/ to http://localhost/blog while still displaying default index.html or index.php or whatever under /blog directory.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • You use the same DirectorySlash option that the OP does not apparently have access too. See below. – animuson Sep 02 '11 at 14:41
  • Same thing, a 500 internal server error. I don't know why this is happening, and this only happens when the DirectorySlash is used. – SkyPilot Sep 02 '11 at 18:59
  • Apache automatically appends trailing slash after directory names in URL so if you want to change that behavior then `DirectorySlash Off` is needed. Can you look into error.log and see that error Apache is writing into it. – anubhava Sep 04 '11 at 18:09