skip to Main Content

In my Laravel project, the .htaccess in the application root redirect to /public. The page loads and I can navigate through the webapp. But https://myproject.com/public/public/mysite is displayed in the URL.

When I refresh the page, I get a 404 error because myproject.com/public/public/ of course doesn’t exist. When I remove the public/public laravel redirects instantly back to https://myproject.com/public/public/mysite.

Where does the public/public come from? I tried some .htaccess configurations, check the .env file, but it exist no logic reason for me why the app redirects to public/public/.

Can anyone help me?

.htaccess in root

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

.htaccess in /public

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

    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]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

.env APP_URL

APP_URL=https://example.com

Anyone an idea? If you need more information, ask me.

Laravel 9.2
PHP 8.2.4

2

Answers


  1. Chosen as BEST ANSWER

    As miken32 advised:

    The web server document root must be the public directory. Laravel comes with an .htaccess file in public already, don't change it. Laravel does NOT come with an .htaccess file in the application root directory. Remove it. – miken32

    Your web server needs to be properly configured. See this answer – miken32

    That was completely right.

    In Strato web hosting, a folder for the domain must be created in the "Webspace" menu tab. The domain must then be set to the desired root directory. I found this in the "Domains" menu tab under "Settings" (cog wheel). In my case I had to select my-created-domainfolder/public there.

    After that php artisan route:clear, php artisan cache:clear and composer dump-autoload and all works as aspected.


  2. You can remove .htaccess from the public directory and add this code to your root .htaccess, this will not let .env be accessible from the URL, and will resolve your current issue.

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
    Options -MultiViews
    </IfModule>
    
    RewriteEngine On
    
    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    # Handle Front Controller...
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    </IfModule>
    
    <Files .env>
        Order allow,deny
        Deny from all
    </Files>
    
    <Files .zip>
        Order allow,deny
        Deny from all
    </Files>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search