1

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>
hakre
  • 193,403
  • 52
  • 435
  • 836
Özgür Can Karagöz
  • 1,039
  • 1
  • 13
  • 32
  • 1
    Please provide your reference implementation for the language handling by subdomain - the documentation of the configuration is missing. And can you add a bit more context? What is your reasoning about what this configuration should do, what does it instead and what do you mean by "redirect"? Normally that means a HTTP redirect in context of a webserver configuration, but there are no redirects configured in your questions configuration code. – hakre Jul 27 '23 at 11:12
  • Does this already answer your question: [RewriteRule that preserves GET parameters](https://stackoverflow.com/q/3321702/367456)? – hakre Jul 27 '23 at 11:26
  • I'm sad. does not answer. Because my problem is to redirect both under language parameter and public. Both do not work at the same time. – Özgür Can Karagöz Jul 27 '23 at 11:42
  • Well, QSA is missing with the L flagged rule. See the linked Q&A. – hakre Jul 27 '23 at 11:45

1 Answers1

0

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.

Joe
  • 4,877
  • 5
  • 30
  • 51