search

Home  >  Q&A  >  body text

Laravel public folder returns 404 error (.htaccess)

<p>I have a .htaccess file. Through this file I can detect the language by subdomain. There is no problem in this regard. However, when I redirect the incoming request to a file under the public folder, I get a 404 error and the language doesn't work properly. </p> <pre class="brush:php;toolbar:false;">SetEnv DEFAULT_LANG en <IfModule mod_rewrite.c> Options FollowSymLinks -Indexes RewriteEngineOn RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteCond %{HTTP_HOST} ^([a-z]{2}).([a-z0-9-] .[a-z] )$ [NC] RewriteRule (.*) - [QSA,E=LANGUAGE:%1] RewriteCond %{ENV:LANGUAGE} ^$ RewriteRule (.*) - [QSA,E=LANGUAGE:en] RewriteCond %{QUERY_STRING} !language= RewriteRule ^(.*)$ $1?language=%{ENV:LANGUAGE} [QSA] RewriteCond %{REQUEST_URI} !^/public RewriteRule ^(.*)$ /public/$1 [L] </IfModule></pre> <p><br /></p>
P粉238433862P粉238433862487 days ago498

reply all(1)I'll reply

  • P粉604669414

    P粉6046694142023-07-29 13:29:04

    Try using the following:

    SetEnv DEFAULT_LANG en
    
    <IfModule mod_rewrite.c>
        Options +FollowSymLinks -Indexes
    
        RewriteEngine On
    
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        RewriteCond %{HTTP_HOST} ^([a-z]{2})\.([a-z0-9-]+\.[a-z]+)$ [NC]
        RewriteRule .* - [QSA,E=LANGUAGE:%1]
    
        RewriteCond %{ENV:LANGUAGE} ^$
        RewriteRule .* - [QSA,E=LANGUAGE:en]
    
        RewriteCond %{QUERY_STRING} !language=
        RewriteRule ^(.*)$ ?language=%{ENV:LANGUAGE} [QSA]
    
        RewriteCond %{REQUEST_URI} !^/public
        RewriteRule ^(.*)$ /public/?language=%{ENV:LANGUAGE} [L,QSA]
    
    </IfModule>

    Using the above, language detection should work properly even when redirecting to files under the public folder. The %{ENV:LANGUAGE} variable will remain unchanged throughout the rewrite, so the correct language parameters should be passed to the file in the public folder.

    reply
    0
  • Cancelreply