0

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

Tenarius
  • 540
  • 1
  • 5
  • 23
  • 1
    The web server document root **must** be the public directory. Laravel comes with [an `.htaccess` file](https://github.com/laravel/laravel/blob/10.x/public/.htaccess) in public already, don't change it. Laravel does NOT come with an `.htaccess` file in the application root directory. Remove it. – miken32 Apr 28 '23 at 15:00
  • And how I make the public diretory as the web server root document? With an `.htaccess` in the applikation root wich refer to public. Without, the app don´t start/display. – Tenarius Apr 28 '23 at 17:22
  • 1
    Your web server needs to be properly configured. See [this answer](https://stackoverflow.com/a/40756853/1255289) – miken32 Apr 28 '23 at 17:51
  • I don't quite understand what this has to do with the `public/public` problem. The `.htaccess` redirect works, the page loads and I can navigate through the webapp. The question is, why is `https://example.com/public/public/mysite` displayed in the URL. – Tenarius Apr 28 '23 at 19:26
  • Which is it? "the page loads and I can navigate through the webapp" or "I get a 404 error"? Regardless, perhaps you should try the two simple steps I've provided. – miken32 Apr 28 '23 at 19:38
  • The page loads and I can navigate through the webapp. But the URL displays `https://example.com/public/public/mysite`, so when I press F5, I have an 404 Error. But when I navigate through the app, all works, while displaying this `/public/public/` in the URL. – Tenarius Apr 28 '23 at 20:27

2 Answers2

0

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.

Tenarius
  • 540
  • 1
  • 5
  • 23
-3

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>