Reputation: 81
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?
Upvotes: 8
Views: 14477
Reputation: 27140
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>
Upvotes: 7
Reputation: 16825
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'); ?>
Upvotes: 7
Reputation: 15796
You wrote:
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.
Did you mean:
When there is request to "mySite.com/forum/rootFile.php"
, I need to
redirect it to "mySite.com/rootFile.php"
which means I need to
redirect request to the parent directory.
...Which in turn means:
When there is request to "/forum/rootFile.php"
, I need to
redirect it to "/rootFile.php"
which means I need to
redirect request to the parent directory.
In your .htaccess
in the /forum
dir try this:
RewriteEngine On
RewriteRule ^rootFileName(.*) /rootFile.php [QSA,R=301,L]
Upvotes: 0