Apache Redirecting outside of DocumentRoot

Posted on

Apache Redirecting outside of DocumentRoot – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, , , , .

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?

Solution :

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'); ?>

Leave a Reply

Your email address will not be published.