1

I have a file structure as follows:

- Site
  - includes
    - page.php
  - pages
    - example.php
  - public
    - .htaccess

.htaccess uses rewrite_module to resolve most requests to page.php.

RewriteRule ^ "../includes/page.php" [L]

page.php parses the original request and includes the proper "page" from the pages directory.

I want initial requests to resolve to /Site/public, so I have the following apache config:

<VirtualHost *:80>
        ServerName example.com
        DocumentRoot /Site/public
</VirtualHost>

This yields 400 Bad Request errors because I'm attempting to redirect to a file outside of DocumentRoot.

I ultimately don't want a user to be able to directly access files outside of public.

Is there a good way to specify that, in terms of execution, /Site is the root, but in terms of serving /Site/public is the root?

1 Answer 1

3

This is pretty common.

Instead of using .htaccess as a router, you should use PHP.

Edit: You CANNOT use .htaccess to route pages outside of docroot.

Keep most your files outside of the docroot, but keep index.php that handles the routing. PHP is able to include/require files outside of the docroot, and this is used pretty extensively in a few PHP frameworks.

- Site
  - includes
    - page.php
  - pages
    - example.php
  - public
    - .htaccess
    - index.php ( <?php require_once('../includes/page.php'); ?>
1
  • I thought it was common! Sounds like the trick is to basically have page.php inside of public, then?
    – slifty
    Commented Mar 21, 2013 at 21:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .