Move Laravel 5 application to server subfolder

So, I developed a fully functional Laravel 5 application and deployed it to my client server using Apache2, PHP5 and MySQL. The application works as expected, and the URLs are correctly rewritten. Now I just need to clone my application to another server that works on exactly the same stack. The problem is that I need to put my application in a subfolder so that it can be accessed through the URLs matching the domain2.com/movedApp pattern.

In other words: The first server responds to:

 domain1.com/my/routes 

The second server should respond to:

 domain2/movedApp/my/routes 

I tried to reproduce exactly the VirtualHost directive, which runs on the first server, adding /movedApp where necessary, and adding RewriteBase to the .htaccess file in the new public folder, but to no avail.

I noticed that routes follow correctly from URLs, for example:

 domain2.com/movedApp/public/index.php/my/routes 

However, I get an 404 error about assets in the Apache2 log.

I hereby inform you of my .htaccess file located in the public folder of my working Laravel application.

.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> 

I also report the configuration file used by the web server to serve my application

default.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> 
+5
source share
1 answer

So, I played a lot with VirtualHost (s), but the best solution I found was not related to them at all, and here it is.

First of all, you need to configure .htaccess files for both the root folder and the public folder . I found a simple working .htaccess file in this answer , which I am reporting here:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule> 

By default .htaccess works fine, instead, for the public folder.

The last step is to configure the routes in routes.php . The workaround that works for me is pretty rough, and maybe you can find an even more sophisticated solution, but still here it is.

 // 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'); }); 

So basically you need to add $base_route to each route in your application. Hope this helps.

+1
source

Source: https://habr.com/ru/post/1247596/


All Articles