skip to Main Content

I have an .htaccess file. With this file, I am detecting the language code by subdomain (HTTP_HOST).

There is no problem here, however, when I redirect incoming requests to the files under the public folder, I get a 404 error and also the language becomes inoperable.

SetEnv DEFAULT_LANG en

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -Indexes

    RewriteEngine On

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

    # Set the language parameter by HTTP_HOST
    RewriteCond %{HTTP_HOST} ^([a-z]{2}).([a-z0-9-]+.[a-z]+)$ [NC]
    RewriteRule (.*) - [QSA,E=LANGUAGE:%1]

    # Set the language parameter default
    RewriteCond %{ENV:LANGUAGE} ^$
    RewriteRule (.*) - [QSA,E=LANGUAGE:en]

    # Set the language query string if absent
    RewriteCond %{QUERY_STRING} !language=
    RewriteRule ^(.*)$ $1?language=%{ENV:LANGUAGE} [QSA]

    # Finally rewrite the request URI to the /public folder
    RewriteCond %{REQUEST_URI} !^/public
    RewriteRule ^(.*)$ /public/$1 [L]

</IfModule>

2

Answers


  1. Chosen as BEST ANSWER

    I solved it with chatgpt's answer.

    You want to move your Laravel application to the root directory without the /public folder and redirect requests correctly. This is not a recommended approach as Laravel is designed to work with the /public folder as the web server's document root. Removing the /public folder might disrupt the core structure of Laravel and lead to unexpected errors. If you want to remove "/public" from your URLs, you should handle it in your server configuration (e.g., Apache VirtualHost configuration). This way, your Laravel application will work correctly without exposing the /public folder in the URL.

    You are currently detecting languages using subdomains and want to hide the language parameter from the URL like tr.domain.com?language=tr. To hide the language parameter from the URL using .htaccess, you can modify your existing rules as follows:

    SetEnv DEFAULT_LANG en
    
    <IfModule mod_rewrite.c>
        Options +FollowSymLinks -Indexes
    
        RewriteEngine On
    
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect requests without the language parameter to include the language parameter
        RewriteCond %{HTTP_HOST} ^([a-z]{2}).([a-z0-9-]+.[a-z]+)$ [NC]
        RewriteRule ^(.*)$ /public/$1?language=%1 [L,QSA]
    
        # Preserve the current language if the language parameter is already present
        RewriteCond %{QUERY_STRING} language=(w+)
        RewriteRule ^ - [E=LANGUAGE:%1]
    
        # Set the default language if the language parameter is missing
        RewriteCond %{ENV:LANGUAGE} ^$
        RewriteRule .* - [QSA,E=LANGUAGE:en]
    
        # Redirect the request to the /public folder
        RewriteCond %{REQUEST_URI} !^/public
        RewriteRule ^(.*)$ /public/$1 [L,QSA]
    
    </IfModule>
    

    This way, users will be able to detect the language without the language parameter in the URL through subdomains, and the language parameter will not be visible in the URL. However, it's worth noting that this approach is not recommended for SEO purposes. For language redirection, more sustainable solutions usually retain language codes in the URL structure, allowing users to select the language directly from the URL.


  2. Try use 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 ^(.*)$ $1?language=%{ENV:LANGUAGE} [QSA]
    
        RewriteCond %{REQUEST_URI} !^/public
        RewriteRule ^(.*)$ /public/$1?language=%{ENV:LANGUAGE} [L,QSA]
    
    </IfModule>
    

    Using the above the language detection should work even when redirecting to the files under the public folder. The %{ENV:LANGUAGE} variable will be preserved throughout the rewriting process, therefore the correct language parameter should be passed to the files in the public folder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search