I have read all the related discussions here with no resolution. I cannot change the virtual host document root on the server as this is a shared hosting environment. Most of the articles I read suggested a .htaccess in the root of the project, this works to a degree. The site works as expected UNTIL you add a trailing slash to the URL like so /test/, and the site redirects you to /public/test. The /public folder is also accessible from the URL bar. To ensure that the site is secure I tested the .env file by going to /.env, thankfully this does present me with a Forbidden error.
I have created a blank testing application on one of my shared hosting servers to test my progress. You will find my root .htaccess file contents and links to different routes to show the behaviour there.
Please have a look here and any suggestions for a fix would be welcomed as I'm reaching my wit's end :(
To install Laravel 10 on shared hosting and handle the issue with the public directory, you can follow these steps:
Upload Your Laravel Application:
Compress your Laravel application into a zip file.
Upload the zip file to your shared hosting account, usually through a cPanel file manager or via FTP.
Extract the zip file to the desired directory on your shared hosting.
Modify the public Directory:
Since you can't change the document root, you'll need to move the contents of the public directory to the root directory of your domain (often public_html or htdocs).
Move the .htaccess and index.php files from the public directory to the root directory.
Update index.php:
Edit the index.php file in the root directory to update the paths to autoload.php and app.php:
After making these changes, visit your website and test to ensure that everything is working correctly.
Check different routes and add trailing slashes to see if the redirection works as expected.
Remember to replace your-laravel-directory with the actual directory name where your Laravel application resides on the server. Also, ensure that your server meets all the requirements for running Laravel 10, such as PHP version and necessary PHP extensions.
@kokoshneta I have FTP and SSH access to my FTP user folder, this is the ~/ home folder. Inside the home folder is symbolic link public_html which is the web server document root. I removed that symbolic link and replaced it with the same path as before but just added the /public part. This did not work as it "moved" the server's document route.
@Samart69 Okay, that sounds like a somewhat odd setup – if public_html is a symlink, that possibly means you don’t actually have direct access to the document root or anything the server serves at all, except through a symlink. That’s… unusual.
If the server is set up so that your document root is actually ~/home/public_html, then you should be able to do the following:
rename your public folder to public_html in your project (before you upload it)
change the public directory in your app to public_html as described in @martinbean’s answer
change the Vite output directory to public_html in your Vite config file (as described here)
make sure the original public_html symlink is back on the server (and that you’ve removed your project from the folder it links to)
upload your entire project to your ~/home folder on the server – assuming your FTP client respects symlinks, that should put your public content inside the symlinked public_html folder
However, if the server is set up so that the actual document root is /something/you/have/no/access/to/public_html, then it likely won’t work, because then Laravel will start looking for classes and the whole framework in /something/you/have/no/access/to rather than ~/home.
@kokoshneta This seems like such a "hacked", I'm sure there are better ways of doing this with .htaccess files. I would prefer not to change any Laravel core files.
@Samart69 No, this is the correct way to do it. Fiddling around with .htaccess files is the hacky workaround. There are lots of issues with .htaccess files, and on many hosts, they can easily be bypassed. Every ‘solution’ that starts with placing your entire application, including all your passwords and sensitive data, inside your server’s document root is not a viable, secure solution.
The solution I suggested does not change any core Laravel files, only files that are yours to change. The bootstrap/app.php file is not a core Laravel file, but a part of your app; the same is true of your Vite config file.
@martinbean Thank you, I tried this but it does not work for me, It also breaks the Vite build process when you run npm run dev. I tried to symlink the public to public_html then the build system and site works BUT I still have the issue of the trailing slashes redirecting to /public/
@Tray2 I tried this solution and it now breaks the urls with trailing slashes. I tried removing the trailing slashes and adding them. The application now redirects the trailing slash URLs to the full FTP user public path like so https://getc8software.com/usr/www/users/ftpusername/public/test Will try some more versions of the public/.htaccess file.
Yes — it is possible to deploy Laravel on shared hosting, even without SSH, Composer or Artisan.
I’ve had to do this multiple times for client projects with tight budgets. The key ideas are:
• Prepare everything locally (composer install --no-dev, build assets, clear caches)
• Move only the public folder contents into public_html
• Keep the Laravel core outside public_html and fix paths in index.php
• Run migrations via a temporary, protected route, then remove it immediately
• Make sure storage and cache folders are writable and PHP version matches Laravel requirements
It’s not ideal compared to VPS, but for MVPs or early client versions it works reliably if done carefully.