.htaccess map子目录作为root

时间:2012-06-17 01:00:19

标签: .htaccess mod-rewrite url-mapping

我发现了一个与此类似的问题,但似乎对我不起作用(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/ 它没有破坏,所以它的外观就像它应该的那样!

1 个答案:

答案 0 :(得分:1)

[L]匹配时,缺少public/标记和重写循环可能是导致网站破坏的原因。如果第二个规则正确完成,那么公共区域的两条规则中的第一条是不必要的:

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]
相关问题