1

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

/home
    /user
        /public_html
            /pics
                /family

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

  1. Serve the index.php file when www.domain.org is requested
  2. Force the user back to public_html when www.domain.org/pics is requested
  3. Allow the user to see the directory contents when www.domain.org/pics/family 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.

2 Answers2

1

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/?$ http://www.domain.org/ [R,L]

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

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

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. http://www.domain.org/pics/pic1.jpg, but will get redirected if attempting to just access the directory.

Jon Lin
  • 1,343
  • 9
  • 21
  • 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
  • Still no joy. Does it matter that index.php contains `header('Location:http://www.domain.org/site')` which is the directory for a Drupal site? – MountainRider Nov 14 '11 at 20:01
  • If it's still serving the ~/public_html/index.html page, then something else is interferring. It doesn't matter what is inside index.php (unless it's redirecting you to index.html, if that's the case then there's the problem). – Jon Lin Nov 14 '11 at 21:55
0

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.

mgorven
  • 30,036
  • 7
  • 76
  • 121