Search code examples
.htaccessdocument-root

Htaccess rewrite rule out of root


I am trying to do a special rewrite rule.
Suppose ROOT as my hosting root.

I have domain.tld set as root on ROOT/folder

When I try this from a script running on the domain.tld root (which is folder):

RewriteRule sth/?$ ../file.php

It doesn't work, cause I am leaving the domain Root. But, since I am on the same hosting, shouldn't it work? Is there any alternative? I once heard about symlinks, do they apply here?

Thanks for your wonderful attention.


Solution

  • since I am on the same hosting, shouldn't it work?

    No, doesn't matter if you are on the hosting service. A site's Document root is the upper-most directory apache will ever be able to access for that site. So when you rewrite a URL to ../file.php, apache can't traverse any higher than the document root. There is very good reason for this as it prevents people with dodgy morals from making apache access files that they're not supposed to see (e.g. ../../../../../../etc/passwd)

    The only thing you can do is put file.php in your document root somewhere, or create a script that includes it:

    require ../file.php
    

    or

    include ../file.php
    

    and then call that script in your rewrite rule.