【问题标题】:.htaccess rewrite dynamically soft link hidden directory.htaccess 重写动态软链接隐藏目录
【发布时间】:2013-04-03 10:58:04
【问题描述】:

我不知道如何表达这个问题,但希望我的描述更能说明我希望达到的目标。

我目前正在构建一个支持多个应用程序的框架。目录结构是这样的:

/applications/

--/admin/
----/www/
------/css/
--------/bootstrap.css
------/img/
--------/logo.png

--/gallery/
----/www/

/system/
/www/ <- web root
--/.htaccess
--/index.php

我正在使用 php 中的 PATH_INFO 数据对“漂亮的 url”进行 url 重写。

site.com/index.php/path/info/data/

将与

相同

site.com/path/info/data/

所以一切都会通过那个 index.php

框架的基本前提是,url的第一段将与应用名称相关:

site.com/admin/ <- will load the admin application
site.com/gallery/ <- will load the gallery application
site.com/ <- will load what ever the default application is (specified in a framework config)

现在这是我想要这个重写规则做的事情(如果可能的话),我会使用一些示例 url 来帮助说明我的观点。

site.com/admin/login/

  1. 规则获取第一段admin

  2. 规则检查应用程序目录中是否存在 admin 文件夹

  3. 如果存在,请使其能够通过 site.com/admin/www/

    访问 admin/www 内容李>
  4. 禁用目录查看,只允许链接到文件,所以site.com/admin/www/img/不可访问,但site.com/admin/www/ img/logo.png 可访问

  5. 只有文件优先于 index.php 重写,所以即使文件夹存在 site.com/admin/www/img/ 也会传递给 index.php,但是site.com/admin/www/img/logo.png 总是引用该文件,除非它不存在,在这种情况下它会被传递给 index.php

为了澄清,这里有一些链接以及我希望他们做什么(使用上面的示例目录列表):

site.com/ < index.php
site.com/admin/ < index.php
site.com/admin/www/ < index.php
site.com/admin/www/nonexistantfile.php < index.php
site.com/gallery/ < index.php
site.com/admin/www/css/ < index.php

site.com/admin/www/css/bootstrap.css < bootstrap.css
site.com/admin/www/img/logo.png < logo.png

虽然我可以使用框架本身做我想做的事,但在每个资产负载上运行 uri 解析引擎和任何其他框架部分会占用太多资源,这就是我想使用 .htaccess 处理这个问题的原因

p>

谢谢

【问题讨论】:

    标签: php .htaccess mod-rewrite symlink hardlink


    【解决方案1】:

    尝试在您的.htaccess 文件中使用它:

    Options +FollowSymlinks
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*) /index.php
    
    RewriteRule %{REQUEST_URI} ([a-z0-9-_]+)\.(php|png|css)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) /index.php
    

    你说你想重写它们,但如果你不想因为它不适合搜索引擎,只需在代码上的 /index.php 之后添加一个 [R] 标志,但不要忘记前面的一个空格标志,使它们重定向。

    【讨论】:

      【解决方案2】:

      会不会是这样的?:

      # File: www/.htaccess
      
      RewriteEngine on
      
      RewriteBase /
      # If a file exists, serve it directly
      RewriteCond %{REQUEST_FILENAME} !-f
      
      # otherwise forward the request to the index.php
      RewriteRule . index.php
      

      确保您已启用mod_rewrite 并为.htaccess 设置适当的AllowOverride 指令才能生效。

      【讨论】:

        猜你喜欢
        • 2015-07-21
        • 1970-01-01
        • 2014-08-06
        • 2016-08-08
        • 2015-07-10
        • 2016-06-26
        • 2014-06-17
        • 2019-04-01
        • 1970-01-01
        相关资源
        最近更新 更多