Laravel – deploy the app to a sub directory?

fmchanprogrammingLeave a Comment

Usually we deploy the app to the root of domain (i.e. domain.dev) but what if we want to deploy to a sub directory (i.e. domain.dev/sub) because sometimes we have more than one applications hosting on the same server with the same domain?

Virtual Host

cd /etc/apache2/sites-available
sudo nano sub1.conf
<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html
  Alias /sub1 /var/www/html/sub1/public
  <Directory /var/www/html/sub1>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
  </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>

DocumentRoot remains unchanged. Directory is the root of your app and remember to add Alias pointing to public folder.

sudo a2dissite lamp-server.conf
sudo a2ensite sub1.conf
sudo service apache2 restart

.htaccess

cd /var/www/html/sub1/public
sudo nano .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder…
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller…
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* — [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteBase /sub1
</IfModule>

Everything is the same as before but just add one line about RewriteBase.

Check your url http://domain.dev/sub1 and it should work.

If the resource files (css/js/image) do not work, check the html code in blade files.

Replace the path to asset embedded

old:
src="/css/style.css"
new:
src="{{asset('css/style.css')}}"

Leave a Reply

Your email address will not be published. Required fields are marked *