【问题标题】:Laravel on Plesk-Server - Only accessible by calling /publicPlesk 服务器上的 Laravel - 只能通过调用 /public 访问
【发布时间】:2016-07-22 05:25:12
【问题描述】:
在设置全新的 Laravel 安装时,我们必须使用 domain.com/public 而不是 domain.com 来运行它。
我搜索了这个问题,发现这可能是因为 mod_rewrite 未启用。但是,我们正在我们的服务器上运行 Plesk,他们在 http://download1.parallels.com/Plesk/PP12/12.0/Doc/en-US/online/plesk-administrator-guide/index.htm?fileName=74206.htm 的文档说:
The mod_rewrite module is enabled by default in Apache that comes with Plesk for Linux. To make use of the functionality, you will need to create an .htaccess file containing the desired rewrite rules - refer to the Apache documentation for more information.
Laravel 工作正常,如果我将 public 的内容移动到主文件夹中,其余的则移动到 /laravel 文件夹中(只需采用主 index.php 文件)。
但是我想保留原始文件结构。
有什么建议吗?
【问题讨论】:
标签:
apache
.htaccess
laravel
mod-rewrite
plesk
【解决方案1】:
只需使用 .htaccess 从 / 重定向到 /public。可能和这个类似
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
【解决方案2】:
您需要 两个 .htaccess 文件。一个在 Laravel 项目 中,另一个在您的 domain.com
中
Laravel 项目 (/var/www/vhosts/domain.com/public/.htaccess)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?$1 [L,QSA]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
主域(/var/www/vhosts/domain.com/.htaccess)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]