.htaccess重写动态软链接隐藏目录


.htaccess rewrite dynamically soft link hidden directory

我不确定如何表达这个问题,但希望我的描述将使我希望实现的目标更有意义。

我正在构建一个支持多个应用程序的框架。目录结构如下:

/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文件夹是否存在于applications目录

  3. 如果存在,使admin/www内容可通过site.com/admin/www/

  4. 访问
  5. 禁用目录查看,只允许链接到文件,因此site.com/admin/www/img/不可访问,但site.com/admin/www/img/logo.png可访问

  6. 只有文件优先于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

来处理这个问题

谢谢

试着在你的.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]标志,但不要忘记在标志之前的一个空格,使它们重定向。

不应该是这样吗?:

# 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,并设置适当的AllowOverride指令以使.htaccess生效