【问题标题】:Include subfolder with .htaccess包含带有 .htaccess 的子文件夹
【发布时间】:2016-06-28 05:31:00
【问题描述】:

我有一个包含 php 文件的子文件夹。

像这样:domain.com/subfolder/file.php

调用 php 文件就像它们在根文件夹中一样非常有用,而不会忽略现有的根文件。

有没有办法通过 .htaccess 包含子文件夹及其所有内容?

【问题讨论】:

    标签: php .htaccess redirect ftp subdirectory


    【解决方案1】:

    您可以在 root/.htaccess 中使用以下规则:

     RewriteEngine on
    
    
     #1--If the request is not for an existent root directory--#
    RewriteCond %{REQUEST_FILENAME} !-d
     #2--And the request is not for an existent root file--#
    RewriteCond %{REQUEST_FILENAME} !-d
    #3--Then, rewrite the request to "/subfolder"--#
    RewriteRule ^([^/]+)/?$ /subfolder/$1 [NC,L]
    

    上面的重写条件对于避免将根文件夹和文件重写到 /subfolder 很重要。

    或者试试这个:

    RewriteEngine on
    #--if /document_root/subfolder/foo is an existent dir--#
    RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -d [OR]
    #--OR /document_root/subfolder/foo is an existent file--#
    RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -f
    #--rewrite "/foo" to "/subfolder/foo--#
    RewriteRule ^(.+)$ /subfolder/$1 [NC,L]
    

    【讨论】:

    • 第二个选项有效!添加更多文件夹怎么样?复制和粘贴相同的重写会做还是会成为服务器的瓶颈?
    【解决方案2】:

    这样的事情怎么样?

    RewriteRule ^subfolder/(.*)$ $1
    

    这应该对我有帮助

    P.S 请确保在 .htaccess 文件中启用并关闭 apache 重写模块

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 2018-09-04
      • 2014-01-14
      • 1970-01-01
      • 2015-09-14
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多