How to safely change laravel root domain from `/` to `/public` folder after deployment in production.

Michael Ishola Sanni
3 min readApr 30, 2021

--

Since i was coming from basic php site background which uses the project root directory as the main entry folder, so i took that mindset into laravel.

i used windows OS with xampp server which requires me to visit all sites at http://localhost/laravelRootFolder.

if i was to follow laravel convention i would have needed to be visiting http://localhost/laravelRootFolder/public which is not url friendly, i was very lazy to run windows CMD in order to use laravel homestead server, instead i decided to tweak the siteRootFolder in order to handle request just as if it was the `public` folder.

Since i couldn’t find any guide on laravel deployment to shared hosting which recommends pointing the domain to paths.../laravelRootFolder/public but instead i used my local dev way and pointed the domain to paths.../laravelRootFolder and the web-service project started functioning in production.

Later i had to change the web-service api url from the project frontend client app from domain.com/api to domain.com/public/api because of some media upload issues.

All seems to be working fine for long until my app needed to use laravel’s horizon.

After supporting horizon from development environment, i deployed new branch with horizon to production but `BOOOOOOOOM` i started getting uncontrollable errors. all routes endpoint were returning 403 Forbidden Errors.

I reported the issue on laravel’s horizon repo but they closed my issue and responded with laravelRootFolder not supported as the main entry folder.

So i decided to point the domain to paths.../laravelRootFolder/public and all seemed to work but my project is a web-service project and not a website which changed url will reflect immediately they revisit the site. So then i reverted the change of url and dropped horizon deployment from production.

Before i could use horizon in production:

  1. i have to change the domain path to paths../laravelRootFolder/public.
  2. find a way to handle apps sending request to the old url such as domain.com/public/api.

I changed the domain path but the main problem lies in finding a way to handle apps sending request to the old url such as domain.com/public/api.

I tried to use .htaccess to redirect all domain.com/public/api to domain.com/api but .htaccess wont redirect POST requests.

Not until i thought of trying to solve the issue with laravel. If i couldn’t solve it outside of laravel then i should try to solve it inside of laravel.

I planned to get all request from old url and remove the public path from the url and allow laravel to handle them as a new request such that domain.com/public/api will become domain.com/api .

Here is my solution.

## Capture requests from old url.

Now my project is functioning fine and using the right domain path.

If your project domain is still pointing to your laravel root directory it is better to make a change now.

I hope i made some senses 😜.

--

--

Michael Ishola Sanni
Michael Ishola Sanni

Responses (1)