To run Laravel Spark in a subdirectory instead of the default ports (80
or 8000
), you need to configure your Laravel application to work correctly under a subdirectory.
Step-by-Step Guide to Run Laravel Spark in a Subdirectory
1. Move Laravel Spark to a Subdirectory
Let's assume your Laravel Spark project is moved to a subdirectory, e.g., /opt/lampp/htdocs/myapp
.
So, your site URL will be something like:
http://localhost/myapp/
2. Update the .env
File
In your .env
file, update the APP_URL
to reflect the subdirectory:
APP_URL=http://localhost/myapp
If you're using HTTPS, update it accordingly:
APP_URL=https://example.com/myapp
3. Update Laravel Routes in routes/web.php
Modify all route definitions to ensure they respect the subdirectory. Instead of:
Route::get('/', function () {
return view('welcome');
});
Use:
Route::prefix('myapp')->group(function () {
Route::get('/', function () {
return view('welcome');
});
// Other routes
Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
});
This ensures that all routes will now be accessible under:
http://localhost/myapp/
http://localhost/myapp/dashboard
4. Update the public
Folder Path
If you are running Laravel Spark in a subdirectory, you need to configure public
as follows:
Option 1: Modify .htaccess
(Recommended)
In the public
folder of Laravel, modify the .htaccess
file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myapp/ # Add this line
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myapp/index.php [L] # Modify this line
</IfModule>
Option 2: Modify server.php
In the server.php
file, update:
$_SERVER['SCRIPT_NAME'] = '/myapp/index.php';
$_SERVER['PHP_SELF'] = '/myapp/index.php';
5. Update Laravel's config/app.php
Open config/app.php
and update:
'url' => env('APP_URL', 'http://localhost/myapp'),
6. Configure Apache for Subdirectory (If using Apache)
Modify your Apache Virtual Host Configuration file (usually in /etc/apache2/sites-available/000-default.conf
for Ubuntu):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /opt/lampp/htdocs/myapp/public
<Directory /opt/lampp/htdocs/myapp/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then restart Apache:
sudo systemctl restart apache2
7. Configure Nginx for Subdirectory (If using Nginx)
Modify your Nginx configuration file (e.g., /etc/nginx/sites-available/default
):
server {
listen 80;
server_name example.com;
root /opt/lampp/htdocs/myapp/public;
index index.php index.html index.htm index.nginx-debian.html;
location /myapp {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Then restart Nginx:
sudo systemctl restart nginx
8. Clear Config & Cache
Run the following Artisan commands to clear the cache:
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan optimize
Final Testing
Now, open your browser and go to:
http://localhost/myapp/
or if using a domain:
http://example.com/myapp/
If everything is set up correctly, Laravel Spark should now work inside the subdirectory.
Additional Fixes (If Required)
If CSS and JS are not loading properly, update your
vite.config.js
ormix.config.js
:mix.setResourceRoot('/myapp/');
Then run:
npm run dev
If you face session issues, update:
SESSION_DOMAIN=localhost
Let me know if you encounter any issues!