2

I need to somehow remove the last slash in my directory URLs. For example, instead of example.com/projects/, I'd like to see example.com/projects. I have been trying many solutions with no luck so far. Here is my .htaccess file currently:

Options +FollowSymLinks
DirectoryIndex index.html

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]

ErrorDocument 404 http://www.example.com/error404.html
3
  • Is /projects a physical directory on the filesystem? Do you only want to remove the trailing slash when the URL does map to a physical directory?
    – MrWhite
    Commented Jan 17, 2017 at 21:56
  • @w3dk /projects is a folder in the root. This folder contains an index.html file which displays when the folder is linked to like this: "olivar.info/projects". I intend on having the index appear as "olivar.info/projects" and other pages within the projects file like "olivar.info/projects/exampleproject".
    – Oli
    Commented Jan 17, 2017 at 22:18
  • Note that the trailing slash is actually the normal thing to do since it denotes a directory. That's why directory access automatically goes to the index file in that directory. That's how it's designed to work. You really shouldn't remove the slash unless it's the name of an actual file they are trying to access.
    – Rob
    Commented Jan 18, 2017 at 1:07

1 Answer 1

0

Presumably you are already linking to the directory URLs without the trailing slash? That is the first step.

Since these are physical directories you need to tell mod_dir to not "fix" these URLs by appending a trailing slash. You can do this with the DirectorySlash directive near the top of your file:

DirectorySlash Off

This means that you now need to manually append the trailing slash with an internal rewrite. Note that mod_dir ordinarily 301 redirects to the slashed URL, so this will have been cached by the browser. Ensure your browser cache is clear before testing.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]

The main problem with this is that the first RewriteCond directive prevents directories from being rewritten, which would seem to be the opposite of what you are trying to achieve. Try the following instead:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
RewriteRule ^(.+?)/?$ /$1/index.html [L]
ErrorDocument 404 http://www.example.com/error404.html

Aside: You should specify a root-relative URL to your error document, like:

ErrorDocument 404 /error404.html

If you use an absolute URL Apache will trigger an external redirect to your error document (instead of an internal subrequest) which is generally undesirable.

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.