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