htaccess公开一些文件夹

时间:2017-04-20 13:03:57

标签: php apache .htaccess mod-rewrite url-rewriting

如何使用.HTACCESS文件从public目录提供PHP页面而不在URL中显示public

我的.HTACCESS文件中包含的内容:

RewriteEngine on
RewriteCond $1 !^bower_components($|/)
RewriteCond $1 !^node_modules($|/)
RewriteRule ^(.*)$ public/$1 [L]

我的项目文件系统包含的内容:

.htaccess
bower_components/
node_modules/
public/
    a/
        index.php
        some_other_page.php
    b/
    c/
src/ <------ should NOT be accessible to public

网址

http://example.com/something/a/
http://example.com/something/a/index.php
http://example.com/something/a/some_other_page.php
http://example.com/something/b/

等等。

上述.HTACCESS适用于所有情况,除非没有尾部斜杠。

这有效:http://example.com/something/a/

这不起作用:http://example.com/something/a

重定向到http://example.com/something/public/a/并发出404 Not Found错误。

如何使用和不使用尾部斜杠来使其工作?

还按照评论中的建议尝试了DirectorySlash指令的值On和Off,但结果没有改变。

在Apache 2.2.15和Apache 2.4.18上都会发生这种情况。

更新

访问时没有斜杠时记录RewriteLogLevel 3:

192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] strip per-dir prefix: /var/www/html/project1/a -> a
192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] strip per-dir prefix: /var/www/html/project1/a -> a
192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] applying pattern '^(.*)$' to uri 'a'
192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (2) [perdir /var/www/html/project1/] rewrite 'a' -> 'public/a'
192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] add per-dir prefix: public/a -> /var/www/html/project1/public/a
192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (2) [perdir /var/www/html/project1/] strip document_root prefix: /var/www/html/project1/public/a -> /project1/public/a
192.168.33.1 - - [20/Apr/2017:12:54:38 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (1) [perdir /var/www/html/project1/] internal redirect with /project1/public/a [INTERNAL REDIRECT]

以下是带有斜杠的日志

192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] add path info postfix: /var/www/html/project1/a -> /var/www/html/project1/a/
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] strip per-dir prefix: /var/www/html/project1/a/ -> a/
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] add path info postfix: /var/www/html/project1/a -> /var/www/html/project1/a/
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] strip per-dir prefix: /var/www/html/project1/a/ -> a/
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] applying pattern '^(.*)$' to uri 'a/'
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (2) [perdir /var/www/html/project1/] rewrite 'a/' -> 'public/a/'
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (3) [perdir /var/www/html/project1/] add per-dir prefix: public/a/ -> /var/www/html/project1/public/a/
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (2) [perdir /var/www/html/project1/] strip document_root prefix: /var/www/html/project1/public/a/ -> /project1/public/a/
192.168.33.1 - - [20/Apr/2017:13:02:45 --0400] [/sid#7f54b8915cf0][rid#7f54b8dc9748/initial] (1) [perdir /var/www/html/project1/] internal redirect with /project1/public/a/ [INTERNAL REDIRECT]

0 个答案:

没有答案