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 (oftenpublic_html
orhtdocs
). - Move the
.htaccess
andindex.php
files from thepublic
directory to the root directory.
- Since you can’t change the document root, you’ll need to move the contents of the
- Update
index.php
:- Edit the
index.php
file in the root directory to update the paths toautoload.php
andapp.php
:
require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php';
Change these lines to (assuming that the rest of your Laravel app is one level above the public_html):Copyrequire __DIR__.'/../your-laravel-directory/vendor/autoload.php'; $app = require_once __DIR__.'/../your-laravel-directory/bootstrap/app.php';
- Edit the
- Create or Modify the Root
.htaccess
File:- If you don’t already have a
.htaccess
file in your root directory, create one. - Add the following rules to handle the redirection and removal of the
/public
from the URL:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/ [L] </IfModule>
- If you’re facing issues with trailing slashes, you can add the following rules to handle that:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] RewriteRule ^(.*)$ public/ [L] </IfModule>
This will remove the trailing slash for non-directory requests and then rewrite the request to thepublic
directory. - If you don’t already have a
- Secure Sensitive Directories:
- To prevent public access to sensitive directories like
/vendor
or your.env
file, you can add additional rules to your.htaccess
file:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI} ^/(vendor|storage|.env)(/|$) RewriteRule ^ - [F,L,NC] </IfModule>
- To prevent public access to sensitive directories like
- Test Your Application:
- 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.
Discover more from Soa Technology | Aditya Website Development Designing Company
Subscribe to get the latest posts sent to your email.