htaccess RewriteRule redirecting to parent directory?

后端 未结 3 929
一向
一向 2020-12-19 05:17

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 fi

相关标签:
3条回答
  • 2020-12-19 05:56

    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]
    
    0 讨论(0)
  • 2020-12-19 05:58

    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>
    
    0 讨论(0)
  • 2020-12-19 06:03

    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'); ?>

    0 讨论(0)
提交回复
热议问题