Laravel is a popular PHP framework known for its flexibility and powerful features. However, when deploying an application developed with Laravel, there are cases where you may want to change the URL structure or adapt to the limitations of a shared hosting environment. In particular, Laravel recommends setting the public directory as the document root of the web server, but this is not always possible on every shared hosting service.

In this article, we will explain the specific steps and methods to meet the following requirements in Laravel 9.

  • Change the current URL from https://hogehoge.com/demo/public/ to https://hogehoge.com/demo/
  • A method to handle it using only .htaccess while keeping the public directory unchanged
  • How to deal with cases where the public directory cannot be directly specified as the document root on shared hosting

Specific Steps to Change the URL

Create an .htaccess File in the Root Directory

Create a new .htaccess file in the root directory of your Laravel project.

Configure the Contents of the .htaccess File

Add the following content to the .htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect requests except the root to the public directory
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L,QSA]

    # Do not redirect if the public directory does not exist
    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d
    RewriteRule ^ - [L]

    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
    RewriteRule ^ - [L]
</IfModule>

Restart the Server

Restart the web server to apply the settings.

Verification

Access the new URL https://hogehoge.com/demo/ and confirm that the website is displayed correctly.

Important Notes

  • Please modify the .htaccess file carefully. Incorrect settings may affect the availability of the website.
  • This method applies when using the Apache web server. If you are using another web server (such as Nginx), you will need to configure it accordingly.

Specific Steps for Handling the public Directory on Shared Hosting

Create a Symbolic Link

If SSH access is available on your shared hosting server, create a symbolic link to the public directory in the root directory.

ln -s /path/to/your/laravel/project/public /path/to/your/document/root/demo

 
This command creates a symbolic link named demo and links it to the public directory.

Subdomain Configuration

Using the control panel of your shared hosting service, configure a new subdomain (e.g., demo.hogehoge.com) and set its document root to the directory pointed to by the symbolic link.

Verification

Access the new subdomain and confirm that the website is displayed correctly.

Important Notes

  • The method using a symbolic link only works if the server supports symbolic links.
  • Subdomain configuration varies depending on the tools or interface provided by your hosting service. If necessary, consult the server documentation or support.

Finally

Changing URLs in Laravel and handling the public directory heavily depend on the environment and configuration of your shared hosting server.
In this article, we provided general guidelines, but in actual implementation you need to consider the specific limitations and features of your server environment. Before making any changes, always take a backup and perform sufficient testing.

 
*If you reuse this content, please do so at your own risk.*