Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Andreas94's avatar

site also accessible from /public

I have a big headache, the site is accessible both from domain.it and from domain.it/public and google indexed both of them! and I don't understand why.

can you help me not to make the site accessible from /public? I don't understand how

0 likes
14 replies
Snapey's avatar

Host the site so that public is the document root

I guess you did some hack to make it work on shared hosting with the whole Laravel app published

Andreas94's avatar

Exactly @snapey , the site works perfectly.

.env etc files are all hidden and cannot be viewed.

I integrated an IP.Board forum platform in Laravel and to have it viewed in domain.it/forum I installed it in public /forum.

then /publish is the main directory of the project... I installed laravel in a DigitalOcean droplet with Plesk

Snapey's avatar

Well I'm afraid you have caused your own problem by having rewrites of requests including public to the index.php in the root folder?

Andreas94's avatar

in the root folder I duplicated the file server.php rename it index.php.

and this I have my htaccess in the root:

<IfModule mod_rewrite.c>
   RewriteEngine On
  
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_URI} =/forum
RewriteRule ^.*[^/]$ /LARACASTS_SNIPPET_PLACEHOLDER/ [L,R=301]
 
   RewriteCond %{REQUEST_URI} !/public/
   RewriteRule ^(.*)$ public/ [L]
</IfModule>

I read on the internet that this is one of Laravel's biggest problems, correctly removing /public the url

siangboon's avatar

subdomain is cheap, why not separate it out....

Andreas94's avatar

@siangboon I did not understand your answer.

the problem is not the forum, but that laravel is accessible from two urls and on google everything is doubled

domain.it domain.it/public

other example:

domain.it/news/1-test domain.it/public/news/1-test

Snapey's avatar

in the root folder I duplicated the file server.php rename it index.php.

don't know what you expect people to tell you...

siangboon's avatar

there is some purposes to have the public folder, if you setup correctly, you wouldn't access your site url with a /public/ also even without follow the link you gave. anyhow this is your site... comment out the rewritecond and rewriterule should solve your problem i guess...

Snapey's avatar

As I said, you set the document root to be the public folder. This means only the contents of the public folder are served by your web server and everything in the Laravel project is private except that folder.

This is the best solution rather than messing with htaccess and renaming files

I hope you don't have phpunit installed on you production machine as it can be hacked giving full access to the server

pdlbibek's avatar

just updating the root .htaccess file would do, you don't have to change server.php

Put this on your htaccess:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On


RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^ [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/ 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
mdrizwan's avatar

That is really cool but this way, servers.php will also be accessed publicly.

christogonus's avatar

Here is how I do it:

Move the complete laravel project content to a directory over the root directory as below:

/home/{cpanel_username}/project-folder

Then I copy all the content of the "public" directory from the laravel directory to "public_html" as below

/home/{cpanel_username}/public_html

Then I update the index.php to reference the correct file parts.

NB: The example file parts here is as on Cpanel. But I trust you know how to translate it to your panel.

Snapey's avatar

why not just create a symlink from public_html to public

I have done this multiple times on cpanel and it always works.

Please or to participate in this conversation.