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?
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'); ?>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With