在子目录&阻止访问它


Installing Laravel in subdirectory & blocking access to it

Laravel在public目录下有HTML文件。因此,如果我使用git将源代码推送到我的站点,我必须通过http://example.com/public访问该站点。但我不想这样。

因此,我将Laravel源代码包含到名为laravel的子目录中,并在站点根目录下创建了一个.htaccess文件,其中包含以下代码:
<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteBase /laravel
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*?)$ public/$1 [L]
</IfModule>

因此,任何对根目录的请求都将被转发到laravel目录中的public目录。

它可以工作,但是任何人都可以通过http://example.com/laravel

访问laravel目录

我不希望他们访问laravel目录。那么,如何使用子目录安装来阻止对laravel目录的访问呢?

我已经尝试在laravel目录的.htaccess文件中添加deny from all,但它使访问根站点也被禁止

当前目录树:

my-site-dir
    |- .git
    |- .gitignore
    |- .htaccess
    |- laravel
        |- app
        |- bootstrap
        |- public
        |- vendor
        |- and rest of Laravel Files

经过一番搜索,我找到了一个解决方案。阻止HTTP请求laravel目录。

laravel子目录下创建.htaccess文件,并放入以下代码:

RewriteEngine on 
RewriteRule $ - [F]

上述规则将显示一个403 Forbidden Error当任何直接请求到laravel目录的文件(包括索引)时,但不会影响根目录的.htaccess文件的mod_rewrite规则

你只需要改变你的DocumentRoot/root。

指向laravel的公共文件夹,例如:

服务器{

Index .php Index .html Index .htm;
    server_name your.name;
    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }
    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
    location ~ '.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    } 

}