Rewrite rules - go beyond docroot

Assuming the following directory structure,

htdocs/
  images/
  css/
  .htaccess
system/
  index.php
  ...

      

I would like to route all incoming requests through this php script. I'm trying to rewrite the rules in htaccess, but I can't seem to be able to route files that are outside of the document root. I couldn't find the reason for this in the apache manuals, so I ended up resorting to the "Include" apache config file where the rules are different.

Is there a way to overwrite it outside of docroot?

+1


source to share


2 answers


What a lot of frameworks do is use rewrite to route all requests to a single file where you can do more complex routing. An example .htaccess file could be:

# ignore anything that an actual file (eg CSS, js, images) 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# redirect all other traffic to the index page
RewriteRule ^.*$ index.php [L]

      



Where index.php is inside your doc root (htdocs / index.php) and from which you can safely include System / index.php where needed.

+4


source


No, you cannot go outside the docroot without potentially huge risk.



You have to put index.php

in a folder htdocs

and overwrite it there, not outside. If you have to rewrite to handle the index by accessing outside of docroot and you've made one small mistake that any hacker rewriting the query string potentially exploited. Not pretty!

+3


source







All Articles