Richard Morris
Richard Morris

Reputation: 31

Remove Trailing Slash from Sub-directory Folder Apache 1.3.42 DirectorySlash Off .htaccess

I’m trying to remove the trailing slash from my sub-directory folders in Apache 1.3.42 however the command DirectorySlash Off is not supported in my version of Apache when I try to add the rule to my .htaccess file.

Currently my links behave like so:

What I want is:

My current .htaccess file looks as follows:

AddType application/x-httpd-php .html

RewriteEngine On
RewriteBase /

#non www to www

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

#removing trailing slash

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

#html

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

#index redirect

#directory remove index.html

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

#directory remove index 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\ HTTP/ 
RewriteRule ^index http://www.example.com/ [R=301,L]

#sub-directory remove index.html

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

#sub-directory remove index

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

#remove .html

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

I do realize this is more likely than not too difficult of a question to solve however I do appreciate you looking at it nevertheless.

Upvotes: 1

Views: 3109

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You need to add a couple of other rules besides removing the trailing slash. With DirectorySlash Off, the redirect from no slash to trailing slash stops, but if you access any directory, it will print the contents of the directory instead of displaying the index file.

This means you must internally add a trailing slash to directories. So change these lines:

#removing trailing slash

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

To:

DirectorySlash Off

#removing trailing slash    
RewriteCond %{THE_REQUEST_FILENAME} \ /(.*)/(\ |$|\?)
RewriteRule ^(.*)/$ $1 [R=301,L]

# internally add the slash back
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L]

Upvotes: 3

Related Questions