Learn how to fix Laravel routes not working on shared hosting. Our Laravel Support team is here to help you with your questions and concerns.
Laravel Routes Not Working on Shared Hosting? Here’s How to Fix It
Laravel is a robust and developer-friendly PHP framework.
If you are having trouble with newly added routes in a Laravel project, you’re in the right place.
This is a common problem, especially for beginners. In this blog, wewill cover three methods for fixing Laravel routes that don’t work on a live server.
An Overview:
- Why Shared Hosting Can Be a Problem for Laravel
- How to Deploy Laravel on Shared Hosting
- How to Fix Laravel Routes Not Working on Shared Hosting
- 1. Clear Route Cache
- 2. Place `index.php` and `.htaccess` in the Same Directory
- 3. Set up a Symbolic Link to `public_html`
- Bonus: Full Laravel Deployment Structure
Why Shared Hosting Can Be a Problem for Laravel
Shared hosting is affordable and accessible, but it can cause trouble with complex applications like those built with Laravel. Here are some of the common issues our experts have come across:
- Laravel apps often require higher CPU and memory resources when handling large datasets or high traffic. Shared hosting environments typically limit resource usage, which can lead to timeouts, crashes, or performance issues.
- We can’t fully customize the server environment without root access.
- Laravel works well with certain PHP extensions and configuration tweaks. However, in a shared hosting setup, we cannot easily modify files like `php.ini` or `.htaccess`, which can break our app if it’s not properly adapted.
How to Deploy Laravel on Shared Hosting
Many hosting providers offer shared hosting. We can run a Laravel app on them with these steps:
- First, zip the Laravel project folder.
- Then, open cPanel > File Manager and upload the zip file.
- Now, unzip `laravelapp.zip` in the hosting root directory.
- Next, head to the public folder inside the Laravel app and move its contents to the public_html folder. Then, delete the public folder.
- At this point, open the public_html/index.php file and update the following lines:
// Change these:
require __DIR__.'/../bootstrap/app.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
// To these:
require __DIR__.'/../laravel/bootstrap/app.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
- Now, check if `.htaccess` exists in `public_html`. If not, create one. You can also enable Show Hidden Files in File Manager to find it.
- Then, use phpMyAdmin to create a new database, import the SQL dump, or run Laravel migrations.
- Remember to grant only the necessary write access to storage and bootstrap/cache.. However, be careful, as this can be a security risk if left unchanged in production. If your app shows syntax-related issues after this step, it might be worth reviewing our guide on Laravel syntax error: unexpected end of file.
- Since .env is usually ignored by version control, we can manually copy it from the local environment or use the `.env.example` as a template.
How to Fix Laravel Routes Not Working on Shared Hosting
1. Clear Route Cache
Although Laravel uses a route cache to improve performance in production, it can also cause issues if you add new routes and the cache isn’t updated.
To resolve this issue, we can regenerate the route cache by running the following Artisan command:
php artisan route:cache Remember to use this command only during deployment, as route caching can cause new routes to be ignored until the cache is refreshed.
If the new routes are still not working, clear the cache first:
php artisan route:clear Then, try accessing the routes again. This often resolves routing issues immediately. However, if you see strange behavior, check out why the error bag may appear empty in Laravel.
2. Place `index.php` and `.htaccess` in the Same Directory
A common mistake made during shared hosting is misplacing the `.htaccess` or `index.php` files.
We can resolve this easily with these steps:
- First, check the location of `index.php`. It should be inside the `public_html` directory.
- Then, make sure `.htaccess` is in the same directory. Laravel uses `.htaccess` for URL routing.
Since .htaccess is a hidden file, it might not appear in cPanel by default.
We can make it visible by heading to File Manager > Settings and enabling “Show Hidden Files (dotfiles).” Then, click Save.
If the `.htaccess` file is missing, create a new file named `.htaccess` inside the same directory as `index.php`.
These small configuration oversights can lead to confusing errors, such as soft delete not restoring properly. If you’ve run into that too, this Laravel soft delete restore error fix may help.
After both files are in place and properly configured, the Laravel routes should function normally. If the error still occurs, try the next method.
3. Set up a Symbolic Link to `public_html`
When Laravel is installed in a subdirectory, the application’s `public` folder needs to be linked to `public_html`. This makes the Laravel app’s entry point accessible on the web.
- Rename `public_html` to something like `public_html_old` to avoid conflicts.
- Then, access the server via SSH.
- Now, run the following command:
ln -s /home/username/laravel_project/public /home/username/public_htmlReplace `username` with the actual cPanel username and `laravel_project` with the name of the Laravel project folder.
This creates a symbolic link from the Laravel app’s `public` folder to the `public_html` directory, allowing web visitors to access our application without issues.
If clearing the route cache and checking file locations do not resolve the issue, inspect Laravel’s logs in storage/logs/laravel.log. These logs can reveal missing files, permission errors, or application exceptions that affect route handling. Next, run php artisan route:list from the Laravel project directory to confirm that the expected routes are registered. Check the HTTP method and URL used in the browser against the registered route. Also, verify that the hosting provider’s PHP version meets your Laravel version’s requirements. These checks help narrow down the cause before you modify .htaccess or change the deployment structure.
Bonus: Full Laravel Deployment Structure
- For a cleaner deployment, our experts suggest structuring the project like this:
/
|-- var
|-- www
|-- laravel
|-- public_html
|----log
- Then adjust these files:
- index.php (inside `public_html`):
require __DIR__.'/../laravel/bootstrap/app.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
- server.php (inside `laravel` folder):
require_once __DIR__.'/../public_html/index.php';
- index.php (inside `public_html`):
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
These methods, whether clearing the route cache, fixing file locations, or setting up a symbolic link, will help get the Laravel site back on track.
In brief, our Support Experts showed you how to proceed when Laravel routes aren’t working on shared hosting.