Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a web site on a shared hosting server. My directory layout looks like this


There is an index.php file in public_html. I need help writing .htaccess rules that will

  1. Serve the index.php file when is requested
  2. Force the user back to public_html when is requested
  3. Allow the user to see the directory contents when is requested

I experimented with a lot of combinations of RewriteCond and RewriteRule, but I don't understand the documentation and examples well enough to know if what I want to do is even possible.

The web server application is some version of Apache.

share|improve this question

1) You can get this one for free by adding a DirectoryIndex index.php in your .htaccess file that's sitting in public_html.

2) To reroute requests to /pics, you can use this set of rewrite rules:

RewriteRule ^pics/?$ [R,L]

3) You'll need to use the with Options:

<Directory "/home/user/public_html/pics/family">
    Options Indexes

You'll need to make sure in your webserver conf or virtualhost conf that you can override options in your .htaccess files. Something like AllowOverride All or AllowOverride Indexes. Also note that with #2, people will still be able to access the files in pics, e.g., but will get redirected if attempting to just access the directory.

share|improve this answer
I put DirectoryIndex index.php in public_html, but it still serves index.html – MountainRider Nov 14 '11 at 19:19
Then replace with this rule: RewriteRule ^$ /index.php [L] – Jon Lin Nov 14 '11 at 19:24
So far, the only success I have is to put Options Indexes in pics and family. My ignorance of the correct syntax for RewriteRule and RewriteCond just causes the server to report 503 or some other error. – MountainRider Nov 14 '11 at 19:25
ok. I have a .htaccess in public_html with only RewriteRule ^$ /index.php [L] and it still serves index.html. Are there any other lines I need to add, like RewriteEngine On or RewriteBase /? – MountainRider Nov 14 '11 at 19:27
You need RewriteEngine On – Jon Lin Nov 14 '11 at 19:31
up vote 0 down vote accepted

I put this in public_html/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} "/pics/" [OR]
RewriteCond %{REQUEST_URI} "/pics/family/"
RewriteRule (.*) $1 [L]

There is nothing in public_html/pics/.htaccess
I put this in public_html/pics/family/.htaccess

Options +Indexes

I removed index.html.

After that, everything worked the way I wanted it to work. I got the idea from this Drupal documentation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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