Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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'); ?>
share|improve this answer
    
I thought it was common! Sounds like the trick is to basically have page.php inside of public, then? –  slifty Mar 21 '13 at 21:04
add comment

Your Answer

 
discard

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.