Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

This question already has an answer here:

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

  1. www.example.com/blog
  2. www.example.com/blog/

Both should show the index page under blog folder.

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

share|improve this question

marked as duplicate by bybe, danlefree Mar 22 '13 at 5:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What have you tried? –  John Conde Mar 19 '13 at 11:44
    
I have tried this =================== #Options -Multiviews -Indexes +FollowSymLinks RewriteEngine On RewriteBase / DirectorySlash Off DirectoryIndex index.html Options -Indexes ========== I am getting 403 forbidden error for example.com/test But it is working fine for www.example.com/test/ –  Ayan Adhikary Mar 19 '13 at 13:01

3 Answers 3

up vote 2 down vote accepted

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

share|improve this answer
    
Thank you very much stephen, it is working for me.Now, I am going to apply other rules in this folder. I am very grateful for your kind help. –  Ayan Adhikary Mar 19 '13 at 13:52
    
Please use the check mark at the top of this answer to accept it. I worked hard for this one. :-) –  Stephen Ostermiller Mar 19 '13 at 13:55
    
Extremely sorry Stephen for late. I have checked you answer. Thanks again a lot! –  Ayan Adhikary Mar 20 '13 at 6:41

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 http://stackoverflow.com/questions/3258879/apache-directoryslash-off-site-breaks it appears that DirectorySlash Off has the side effect of preventing DirectoryIndex from working.

share|improve this answer
    
Good point, the best method for him is most likely disable directoryIndex and then use a conditional rewrite for removing the tailing slash. –  bybe Mar 19 '13 at 12:45
    
Thanks for your kind answer,. But, it is not working for me. I have written the following coding in the inner folder. ======== #Options -Multiviews -Indexes +FollowSymLinks RewriteEngine On RewriteBase / DirectorySlash Off DirectoryIndex index.html Options -Indexes ======== It is giving 403 forbidden when I am trying www.example/test But, www.example.com/test/ is working fine. Please do kindly help –  Ayan Adhikary Mar 19 '13 at 12:56
    
Stop using DirectorySlash Off. –  Stephen Ostermiller Mar 19 '13 at 12:58
    
Also, you could edit your post to say exactly what you want to happen at each of /blog and /blog/. Which do you want to show the index document? Which do you want to redirect? –  Stephen Ostermiller Mar 19 '13 at 12:59
1  
For search engine optimizaton (SEO) purposes, you usually want to make sure you have a single canonical URL. I would suggest using redirects instead of showing the same document at two different URLs. DirectorySlash On does the correct thing for SEO. What is the reason that you don't want a redirect? –  Stephen Ostermiller Mar 19 '13 at 13:17

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 ^(.+)/$ http://www.example.com/$1 [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 ^(.*)\/(\?.*)?$ $1$2 [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 
share|improve this answer
    
I have tried the first one. ===================== RewriteEngine On RewriteBase / DirectorySlash Off # remove trailing slash RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L] ================ But it is automatically adding slash when browsing with www.example.com/test. But, I dont want this redirection. I want to show index.html file under test sub folder when trying both the follwing urls. www.example.com/test and www.example.com/test/ –  Ayan Adhikary Mar 19 '13 at 13:18
    
So you want it to be example.com/test/index.html? If so then you are removing it via index.html -f... for the sake of people answering please could you edit your question and list everything you have in your .htaccess file –  bybe Mar 19 '13 at 13:27
    
Can you please suggest me the coding how can I keep www.example.com/test only. When ever, any one will browse www.example.com/test/ , it should then show www.example.com/test url. But www.example.com/test/somepage.html will work fine. –  Ayan Adhikary Mar 19 '13 at 13:34
    
You really need to edit your question. Your adding things that was not originally asked in your question. –  bybe Mar 19 '13 at 13:37
    
I will do it. But, Can you please answer my question if you can –  Ayan Adhikary Mar 19 '13 at 13:44

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