因此,我开发了一个功能齐全的Laravel 5应用程序,并将其部署到客户的服务器上,运行Apache2,PHP5和MySQL。应用按预期工作,URL 已正确重写。我现在只需将我的应用程序克隆到另一台运行完全相同堆栈的服务器即可。问题是我需要将我的应用程序放在一个子文件夹中,以便可以通过与模式domain2.com/movedApp
匹配的 URL 访问它。
换句话说:第一台服务器响应:
domain1.com/my/routes
第二台服务器需要响应:
domain2/movedApp/my/routes
我试图准确地重现在第一台服务器上工作的 VirtualHost 指令,在需要时添加/movedApp
并将RewriteBase
添加到新 public
文件夹中的.htaccess
文件中,但无济于事。
我注意到路由是从以下 URL 正确遵循的:
domain2.com/movedApp/public/index.php/my/routes
我仍然在 Apache2 日志中的资产上收到 404 错误。
我特此报告我的.htaccess
文件位于我的工作Laravel应用程序的public
文件夹中。
.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
我还报告 Web 服务器用于为我的应用程序提供服务的配置文件
默认.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/galmaster/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/galmaster/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
所以,我一直在摆弄VirtualHost(s),但我找到的最佳解决方案根本不涉及它们,就在这里。
首先要做的是为应用的根文件夹及其公用文件夹配置.htaccess
文件。我在这个答案中找到了一个简单的工作.htaccess
文件,我在这里报告:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
相反,默认.htaccess
适用于public
文件夹。
最后一步是在 routes.php
中正确配置路由。对我有用的解决方法非常粗糙,也许可以找到一些更精致的解决方案,但仍然在这里。
// Get base (root) route
$base_route = basename(base_path());
Route::get($base_route, function () {
return view('welcome');
});
Route::get($base_route.'/myRoute', function() {
return view('myRoute');
});
所以基本上你需要在应用程序中的每个路线前面加上$base_route
。希望这有帮助。