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

Published 2 years ago by charlesBR

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.

bashy
bashy
2 years ago (1,090,090 XP)

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.

christopher

Maybe this helps you: http://laravel-recipes.com/recipes/25/creating-an-apache-virtualhost Seems like your /public folder is not correctly handled by the .htaccess / vhost.

pmall
pmall
2 years ago (580,045 XP)

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

charlesBR

@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

@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
bashy
2 years ago (1,090,090 XP)

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

@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
Ibbe
2 years ago (61,895 XP)

Add the following to your .htaccess

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

@charlesBR This has been asked (and answered) many times before: https://laracasts.com/discuss?search=remove+public

leomarquine

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>
santosh.khanal55@gmail.com

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

@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
bashy
9 months ago (1,090,090 XP)

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

Snapey
Snapey
9 months ago (683,015 XP)

@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.

johnef_sh

@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.

Please sign in or create an account to participate in this conversation.