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

charlesBR's avatar

Removing "/public" from URL (Where does Laravel get the base URL from?)

I'm deploying a new app today. It works great on my local machine with Homestead but not on production server. The problem is with the url() function. It's appending a /public after every generated URLs. Check it out: http://apps.mppr.mp.br/concurso_estagiario/

See? The links and assets are broken due to that additional /public.

How can I remove it?

PS: I'm not able to change the DocumentRoot on the server, because there are multiple applications on this virtual host. Neither could I solve it using alias. I'm not allowed to use .htaccess files either due to internal politics, but I have broad access to make changes to the virtual hosts file.

0 likes
22 replies
bashy's avatar

Did you setup the subdirectory properly? Looks like you have an issue with your htaccess file adding the public segment onto the URL.

Please post your Apache Virtual Host and htaccess so we can see what you've done.

pmall's avatar

You can change the virtualhost but not the document root ? Event if it is in your space ?

charlesBR's avatar

@bashy This is what I've got. I don't have an .htaccess

<Directory /var/www/concurso_estagiario/public/>
        RewriteEngine On
        RewriteBase /concurso_estagiario/public/

        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ /$1 [L,R=301]

        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
</Directory>
charlesBR's avatar

@pmall I can create new <Directory> entries, but there are several applications hosted with this same subdomain apps.mppr.mp.br. If I changed the DocumentRoot, I'd break the other applications.

bashy's avatar

So why can't you just do Directory /concurso_estagiario/ {} and put the document root as the public folder. No need to mess around with rewrites.

charlesBR's avatar

@bashy That's Nginx syntax, right? I'm no pro at web server stuff. Any leads on how could I do that on an Apache server?

Thanks in advance.

Ibbe's avatar

Add the following to your .htaccess

<IfModule mod_rewrite.c>
   RewriteEngine On 
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
leomarquine's avatar

You can make an Apache Alias pointing /concurso_estagiario to /concurso_estagiario/public (full path needed). For example: Alias /concurso_estagiario /var/www/concurso_estagiario/public

And change your htaccess to Laravel default + RewriteBase:

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

    RewriteEngine On

    RewriteBase /concurso_estagiario

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
khanalpride's avatar

I also got same problem.

My configuration is like this.

I already have old app with many files index.php .. .. header.php on my public directory

I have now added laravel app to one folder "laravelapp" just outside the public folder which happen to be public_html in this case.

So Folder structure is like this

laravelapp
    app
    bootstrap
    ...
    ...
public_html
    old files and folder here
    laravelpublic
        css
        js
        index.php
        .htaccess

Now when I go to the public directory its fine. Its loading old pages. but when I go to (mysite.com/laravelpublic) its not loading. any changes I need to make to .htaccess

johnef_sh's avatar

@bashy please review this solution and tell me is it safe to do that or not

        a. Create new folder local.
        b. Move all project into the folder expect public folder.
        c. Move all the content of public folder to project root.
        d. Edit the index file.

by the way it's working great

bashy's avatar

@johnef_sh Well, I wouldn't do that since it could become unsecure (you would obviously stop people viewing files inside laravel system folders).

1 like
Snapey's avatar

@johnef_sh If your folder name is discovered then someone can dump config files or .env for instance. You want your code and especially your config outside of the publicly accessible space.

1 like
johnef_sh's avatar

@bashy @Snapey thanks guys for the suggesting, I saw a lot of solutions around this issue, as I don't want to keep public in my URL in your opinions which is the best and safe solutions, ah I have try to edit the .htaccess but it did not work as expected.

Can you please guide me to the best solution. what should you suggesting to do that. thanks and many regards.

2 likes
bashy's avatar

@johnef_sh You want to alias the URI segment to another folder basically. What web server software are you using?

johnef_sh's avatar

@bashy good morning, I am using WAMP server as my local server.

btw I read what @Snapey has written yesterday If your folder name is discovered then someone can dump config files or .env for instance.

anyway I downloaded a fresh Laravel5.2 project and I was able to access the .env from browser by it's default path, kindly give me explanation.

journey4tech's avatar

i just copy the .htacess from public folder and paste in root folder. than i rename server.php to index.php ( server.php belog to root folder) By doing to i can access. www.example.com ( it's work fine ) But www.example.com/public is also opening .

I dont want to keep /public in my url . if any one enter www.example.com/public I want to redirect to www. example.com

Please give me soultion. Thank you .

Snapey's avatar

@journey4tech That is really bad practice and I would appreciate you not telling others to do this.

You leave the framework exposed and yourwebsite.com/.env will just display all your secrets.

There are too many insecure sites out there for this not to be a known opportunity for hackers.

The framework is architected in this way for a specific reason. It is so that private files are not publicly accessible. Leave it that way.

sanjayacloud's avatar

Solution for this issue

rename server.php to index.php at your root directory

Then copy .htaccess file from public folder to root directory. And add below code for that

Options -MultiViews -Indexes

 

RewriteEngine On

 

# Handle Authorization Header

RewriteCond %{HTTP:Authorization} .

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

 

# Redirect Trailing Slashes If Not A Folder...

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} (.+)/$

RewriteRule ^ %1 [L,R=301]

 

# Handle Front Controller...

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]

 

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !^/public/

RewriteRule ^(css|js|images)/(.*)$ public// [L,NC]

Please or to participate in this conversation.