Htaccess RewriteRule redirect to parent directory?

I cannot figure out how to redirect to the parent directory in this case: I have a root directory with a .htaccess file where rewriteEngine is included. In this directory, I have a rootFile.php file. In addition, in this directory I have a directory called "forum", with another .htaccess file.

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

/forum/.htaccess:

RewriteEngine on RewriteBase /forum/ RewriteRule ^sitemap /forum/map.php [L] ... 

I tried:

 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 redirects from / forum / rootFileName to / forum / rootFile, but / rootFile never works.

Where is my mistake?

+6
source share
3 answers

You cannot rewrite to an external root directory. This is a safety thing.

You can simply create rootFile.php containing only <?php include('../rootfile.php'); ?> <?php include('../rootfile.php'); ?>

+7
source

You cannot rewrite on paths that are outside the root of the document for security reasons. In fact, this should be avoided because it is not a good practice.

In any case, in order to answer your question, a workaround can create a symbolic link in the root of your document that points to the target / parent directory, but you must protect it from direct access.

Let me give you an example. Suppose our document root is /var/www/project1 and what you want to rewrite to /var/www/project2/target.php

Then you need to create a symbolic link inside /var/www/project1 that points to /var/www/project2 with the name (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> 
+3
source

You wrote:

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

You meant:

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

... This, in turn, means:

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

In .htaccess in /forum do the following:

 RewriteEngine On RewriteRule ^rootFileName(.*) /rootFile.php [QSA,R=301,L] 
0
source

Source: https://habr.com/ru/post/905509/


All Articles