【问题标题】:.htaccess map subdirectory as root.htaccess 以 root 身份映射子目录
【发布时间】:2012-06-19 13:21:30
【问题描述】:

我发现了一个与此类似的问题,但似乎对我不起作用(Make web root folder a sub-folder with .htaccess?)。因此,在找不到任何问题的答案后,我寻求帮助——我花了 2 个小时在 stackoverflow 上浏览了 200 多页的结果,然后才放弃。总之……

我正在创建一个小型 CMS,并且我的目录结构设置如下:

application
-- html
-- -- public
-- -- -- theme
-- -- -- -- css
-- -- -- -- images
-- -- -- -- js
-- -- -- forum
-- -- -- -- index.php
-- -- -- -- viewforum.php
-- -- -- -- viewthread.php
-- -- -- -- comment.php
-- -- -- index.php
-- -- -- login.php
-- -- -- profile.php
-- -- admincp
-- -- -- theme
-- -- -- -- css
-- -- -- -- images
-- -- -- -- js
-- -- -- index.php
-- -- -- manage.php
-- -- -- users.php
-- -- -- settings.php
-- plugins
library

我想要完成的是将 mydomain.com/application/html/public/index.php 映射到 mydomain.com/index.php 并使用 admincp 映射其 mydomain.com/admincp/index.php。这样看起来公共目录中的所有内容都位于根目录中。

当我查看 mydomain.com 时,它显示来自 ../public/ 的 index.php 文件,但是当我尝试 mydomain.com/index.php 时,我得到一个未找到的错误。当我转到 mydomain.com/admincp 或 mydomain.com/admincp/manage.php 时,它可以工作——如果你知道一种使它更好的方法,请告诉我。 :)

RewriteEngine on
RewriteRule ^$ application/html/public/
#RewriteRule ^(.*)$ application/html/public/$1 # Breaks site

RewriteCond  %{DOCUMENT_ROOT}/admincp  !-f
RewriteRule ^admincp/(.*)? application/html/admincp/$1
RewriteRule ^admincp$ admincp/ [L]

如果我能在这个问题上获得任何帮助、建议和讽刺,我将不胜感激。

-- 编辑--

@Michael 为我提供了一个符合我目的的答案 结果是这样的:

RewriteEngine on
# Don't need this...
#RewriteRule ^$ application/html/public/

# Need [L] and a RewriteCond
# Don't do the rewrite if this is an actual existing file
# such as what is rewritten into application/html/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# And don't write admincp into public
RewriteCond %{REQUEST_FILENAME} !admincp
# With the above conditions met, write anything else into the public dir
RewriteRule ^(.*)$ application/html/public/$1 [L]

# Then just write admincp into application/html/admincp
RewriteRule ^admincp/(.*) application/html/admincp/$1 [L] 

现在我只需要修复查看根目录时显示我的目录索引的问题--mydomain.com/

-- 编辑--

当我重新添加时 RewriteRule ^$ application/html/public/ 它没有损坏,所以它看起来像它应该的那样工作!

【问题讨论】:

    标签: .htaccess mod-rewrite url-mapping


    【解决方案1】:

    [L] 匹配时缺少[L] 标志和重写循环,这可能是您的网站中断的原因。如果第二条正确执行,则不需要公共区域的两条规则中的第一条:

    RewriteEngine on
    # Don't need this...
    #RewriteRule ^$ application/html/public/
    
    # Need [L] and a RewriteCond
    # Don't do the rewrite if this is an actual existing file
    # such as what is rewritten into application/html/public
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # And don't write admincp or others into public
    
    # Add each area you need to exclude into the () below
    RewriteCond %{REQUEST_FILENAME} !(admincp|formus|otherarea1|otherarea2)
    
    # With the above conditions met, write anything else into the public dir
    RewriteRule ^(.*)$ application/html/public/$1 [L]
    
    # Then just write admincp into application/html/admincp
    #RewriteRule ^admincp/(.*) application/html/admincp/$1 [L]
    # Update: Instead of the above rule, if you need additional rules like forums
    # make the final rule generic to capture the directory in ([^/]+):
    RewriteRule ^([^/]+)/(.*) application/html/$1/$2 [L]
    

    【讨论】:

    • 有效!但是当我添加像RewriteRule ^forum/$ application/html/public/forum.php [L] 这样的东西时,新规则不起作用。 :(
    • 谢谢,它有效。我开始更好地理解 htaccess。
    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 2019-05-16
    • 2017-10-04
    • 2017-09-08
    相关资源
    最近更新 更多