Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess RewriteRule redirecting to parent directory?

I cant understand how to redirect to the parent directory in this case: I have a root directory with .htaccess file, where rewriteEngine is on. In this directory I have a file rootFile.php. Also, in this directory I have a directory "forum", with another .htaccess file in it.

When there is request to "forum.mySite.com/rootFile.php", I need to redirect it to "mySite.com/rootFile.php" which means I need to redirect request to the parent directory.

/forum/.htaccess:

RewriteEngine on

RewriteBase /forum/

RewriteRule ^sitemap                    /forum/map.php                [L]
...

I tried to:

RewriteEngine on

RewriteBase /forum/

RewriteRule ^rootFileName                   ../rootFile.php            [L]
\# or so RewriteRule ^rootFileName           /rootFile.php              [L]
\# or so RewriteRule ^rootFileName           rootFile.php               [L]

RewriteRule ^sitemap                    /forum/map.php             [L]

Also tried this one:

RewriteEngine on

RewriteBase /

RewriteRule ^rootFileName                   ../rootFile.php            [L]
\# or so RewriteRule ^rootFileName           /rootFile.php             [L]
\# or so RewriteRule ^rootFileName           rootFile.php              [L]

RewriteBase /forum/

RewriteRule ^sitemap                    /forum/map.php             [L]

And it always redirect from /forum/rootFileName to /forum/rootFile, but never to /rootFile.

Where is my mistake?

like image 938
Антон Кириченко Avatar asked Jan 09 '12 10:01

Антон Кириченко


2 Answers

You can't rewrite to outside the document-root. This is a security thing.

You could just create a rootFile.php that only contains <?php include('../rootfile.php'); ?>

like image 110
Gerben Avatar answered Nov 20 '22 16:11

Gerben


You can't rewrite to paths that are outside your document root for security reasons. In fact this should be avoided because it is not a good practice.

Anyway, to answer your question, a workaround might be creating a symlink in your document root that points to the target/parent directory, but you should protect it from direct accesses.

Let me do an example. Assume that our document root is /var/www/project1 and that you want to rewrite on /var/www/project2/target.php

Then, you have to create a symlink inside /var/www/project1 that points to /var/www/project2 named (for example) p2.

This should be your /var/www/project1/.htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI} /hello/world
    RewriteRule ^(.*)$ p2/target.php [QSA,L] # p2 is the symlink name!
</IfModule>
like image 33
Francesco Casula Avatar answered Nov 20 '22 16:11

Francesco Casula