Mobile app version of vmapp.org
Login or Join
Sims2060225

: Removing trailing slash for sub directory url indexing I have a site with multiple different sites in sub-directories. I want to view the sub directory in two formats. For example: www.example.com/blog

@Sims2060225

Posted in: #WebHosting

I have a site with multiple different sites in sub-directories. I want to view the sub directory in two formats. For example:

example.com/blog www.example.com/blog/


Both should show the index page under blog folder.

After turning DirectorySlash off, redirection is off from example.com/blog to example.com/blog/ but in example.com/blog, all contents of that folder are showing instead of the index file.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Sims2060225

3 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

I think I understand what you want better now. Let me recap:


/test should show the contents of /test/index.html
/test/ should redirect to /test
/test/index.html should redirect to /test
/test/something.html should show its own contents


You can do this with the knowledge that I gained by asking about how to do something similar here: How to rewrite to a script and also redirect away from that script using .htaccess while avoiding infinite loops

This is what you can put in .htaccess that should work for you:

DirectorySlash Off
RewriteEngine on

RewriteRule ^test$ /test/index.html [L,E=LOOP:1]

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^test/$ /test [R=301,L]

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^test/index.html$ /test [R=301,L]


I tested this on my server, it satisfies all the conditions that I outlined above when I have a /test/ directory containing the files index.html and something.html

10% popularity Vote Up Vote Down


 

@Speyer207

DirectorySlash off is a funny one to be honest because often I can't get it work and I just use this plain old simple rewrite condition which does the job just superbly.

RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(.+)/$ www.example.com/ [R=301,L]


An alternative method using DirectorySlash Off can be found here Pro Webmasters - How to remove trailing slashes from URL with htaccess.

RewriteEngine On
RewriteBase /
DirectorySlash Off

# remove trailing slash
RewriteRule ^(.*)/(?.*)?$ [R=301,L]


Disable Directory Browsing with Virtual Host Config Files

You can disable the directory listing in the main config in your apache setup like so:

<Directory "/var/www/">
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>


If you do not have access to this file i.e your using a shared web host then add the following to your htaccess file:

Options -Indexes

10% popularity Vote Up Vote Down


 

@Megan663

Which file is shown as the default page in a directory is controlled by two settings in Apache. These settings can either be set in the httpd.conf file or in a .htaccess file.

# DirectoryIndex controls which file is chosen as the default page in a directory
DirectoryIndex index.html index.htm index.php welcome.html

# The Indexes option controls whether or not a file listing is shown.
# This usually takes effect only when a DirectoryIndex file cannot be found.
# -Indexes turns off the file list, +Indexes turns it on.
Options -Indexes


Based on this question stackoverflow.com/questions/3258879/apache-directoryslash-off-site-breaks it appears that DirectorySlash Off has the side effect of preventing DirectoryIndex from working.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme