Forum Moderators: phranque
I need to make a redirection in apache, so the user is redirected according to his/her browser language settings to the appropriate subdirectory, where the index.html file in the according language is stored. I use AliasMatch because I don`t want user to see the whole path for the webproject.
So I edited the httpd.conf with following 2 lines:
AliasMatch ^(?:/(?:enŠesŠfrŠjaŠkoŠru))?(/.*)?$ "/usr/local/apache2/htdocs/MyProject/eng$1"
AliasMatch ^(?:/(?:de))?(/.*)?$ "/usr/local/apache2/htdocs/MyProject/de$1"
The problem is as following:
the apache doesn`t evaluate the second alias match. It redirects all requests to the english version, for all of the language settings.
I suppose, I should design the regular expression, so that apache woudn`t be able to execute the first AliasMatch in case the given language settings are not available.
Woulld you give me any hints, HOW I can achieve this?
Thanks for your help
Welcome to WebmasterWorld!
It is not obvious to me how you are getting the browser language preferences with this approach, so this answer may be totally off-topic. A more common method would be to use mod_rewrite in conjunction with cookies. The initial language setting would be taken from the browser's Accept-Language header (if present). In addition, you should provide a language selection function, supported by a cookie, to allow the user to select a diffferent language; You cannot assume that the visitor has control over the browser settings, as this may not be his/her machine. It may be a work machine where the settings are controlled by the IT department, or it may be a machine in an internet cafe where the settings have been modifed to a language not spoken by the current user. And there are many users who don't know that they can set a language preference at all. Therefore, a "manual" setting is to be preferred over any automatic function for usability reasons.
In this case, and assuming that you set a cookie called "lang_pref", the code might look something like this:
# Case: No language preference cookie set - Use browser language preference
RewriteCond %{QUERY_STRING} !lang_pref=[a-z]{2}
RewriteCond %{HTTP:Accept-Language} (de¦enŠesŠfrŠjaŠkoŠru)
RewriteRule ^/([^.]+\.html)$ /%1/$1 [L]
#
# Case: Language preference cookie set - Use cookie
RewriteCond %{QUERY_STRING} lang_pref=(de¦enŠesŠfrŠjaŠkoŠru)
RewriteRule ^/([^.]+\.html)$ /%1/$1 [L]
#
# Case: No language preference - Use default language
RewriteRule ^/([^.]+\.html)$ /en/$1 [L]
Another thing to look into is Content Negotiation -- For example, see Options MultiViews in the Apache documentation.
Jim
thanks for your help. I`ve got another Question:
my Apache (2.0.54) doesn`t like the RewriteCond command in httpd.conf, it sais "RewriteCond is an invalid command". Should I install some additional support for apache to understand this?
Best regards,
Michael
You must have mod_rewrite installed and loaded, and you must have appropriate settings of AllowOverride and Options to allow mod_rewrite to execute. Option FollowSymLinks or option SymLinksIfOwnerMatch is required to enable mod_rewrite.
Your server error log should contain useful info on this problem, unless you've got the loglevel set too high.
Jim