This thread is resolved. Here is a description of the problem and solution.
Problem: Language redirection
Solution: The client was looking for an option to redirect to language directory when *Use directory for default language* option is selected for WPML>>Languages>>Language URL format>>Different languages in directories. Recommended the following as a workaround.
1. In WPML -> Languages :
- Choose 'Different languages in directories'
- Enable 'Use directory for default language'
- Select 'HTML file'
- Insert 'lang.php' inside the text field below
2. In your WordPress installation root directory, the same where you can find your wp-config.php file:
- add a new file and name it 'lang.php'
- edit this file with a text editor and insert the following code (change the URL according to your preferred default language). The following code will help him to redirect according to the browser language.
<?php if(substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) == 'de' ) { header( 'Location: https://yoursite.com/de/' ); }else if( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) == 'fr' ) { header( 'Location: https://yoursite.com/fr/' ); }else { header( 'Location: https://yoursite.com/en/' ); } ?>
Relevant Documentation:
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
Related documentation:
This topic contains 12 replies, has 4 voices.
Last updated by phoenixM 3 years, 9 months ago.
Assigned support staff: Bigul.
Author | Posts |
---|---|
May 24, 2017 at 10:08 am #1283486 | |
pierS-2 |
Hi, I have to know how we can redirect directly into browser language folder without loading the default first and then redirect to browser language that is a bad user experience. We use default language English in folder /en REgards |
May 24, 2017 at 5:15 pm #1283876 | |
Bigul Supporter
Languages: English (English ) Timezone: Asia/Kolkata (GMT+05:30) |
Hi , Thank you for contacting the WPML support. We are happy to help you. Hope you are using the latest version of WPML(3.7.1) and WordPress(4.7.5). To help you faster, I've enabled debug information for this support ticket. Please see this link for how to get this information from your site and give it to us: http://wpml.org/faq/provide-debug-information-faster-support/ We have an option for Automatic redirect based on the browser language. Please check the following tutorial for more details and let me know your feedback. -- Bigul |
May 24, 2017 at 5:40 pm #1283896 | |
pierS-2 |
Hi, I now that, wew don't want to use javascript, but please read my question and let me know how to get direct language browser loaded. |
May 24, 2017 at 5:44 pm #1283901 | |
pierS-2 |
For example how polylang manage redirection wiht http request |
May 25, 2017 at 8:40 am #1284263 | |
Bigul Supporter
Languages: English (English ) Timezone: Asia/Kolkata (GMT+05:30) |
Hi, Hope you are trying to achieve the following. Please confirm and correct me with more details If I am wrong. Also, share me the debug information for more clarity. You have selected *Use directory for default language* option of WPML>>Languages>>Language URL format>>Different languages in directories. For an example, your site will be *example.com*. Your primary language is English and secondary language is *French*. Now you want to redirect user to *example.com/en* when visiting *example.com*. If so, please choose *HTML file* option of WPML>>Languages>>Language URL format>>Different languages in directories>>Use directory for default language. Then create a PHP file *redirect.php* with the following code and place the file in root directory of the site. Add *redirect.php* in *HTML file* field and save the changes. <?php header('Location: <em><u>hidden link</u></em>'); exit; ?> Please check this tutorial for more details -- https://wpml.org/documentation/getting-started-guide/language-setup/directory-default-language/ -- Bigul |
May 25, 2017 at 8:44 am #1284267 | |
pierS-2 |
This is already done! Yes we have these settings now. BUT we don't want to use your WPML javascript redirection. We want to use href on server like for Polylang does. Please advise how to use. regards |
May 25, 2017 at 12:26 pm #1284480 | |
Bigul Supporter
Languages: English (English ) Timezone: Asia/Kolkata (GMT+05:30) |
Hi, For your kind information, right now this is the only option we having and it is an HTTP request. Please try it and let me know your feedback. 1. In WPML -> Languages : 2. In your WordPress installation root directory, the same where you can find your wp-config.php file: <?php header( 'Location: <em><u>hidden link</u></em>' ) ; ?> I will be forwarding the details to our development team as a feature request to add an option like the following if we are choosing *Different languages in directories*. - HTML file -- Bigul |
May 25, 2017 at 12:47 pm #1284503 | |
pierS-2 |
Again... this is the exact settings we have now. I meant how to manage browser redirection on the lang.php, I suppose we have to disable 'Redirect visitors based on browser language only if translations exist' as it is for javascript. Please let me know php code to add in lang.php : If browser language is German for example,then header should be: header( 'Location: hidden link' ) ; ?> If language is French then: header( 'Location: hidden link' ) ; ?> |
May 25, 2017 at 2:23 pm #1284619 | |
Bigul Supporter
Languages: English (English ) Timezone: Asia/Kolkata (GMT+05:30) |
Hi, Please do the following modifications in the *lang.php* file and make sure you are getting the expected result now. *From* <?php header( 'Location: <em><u>hidden link</u></em>' ) ; ?> *To* <?php if(substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) == 'de' ) { header( 'Location: <em><u>hidden link</u></em>' ); }else if( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) == 'fr' ) { header( 'Location: <em><u>hidden link</u></em>' ); }else { header( 'Location: <em><u>hidden link</u></em>' ); } ?> -- Bigul |
May 25, 2017 at 2:56 pm #1284658 | |
pierS-2 |
Finally it works |
June 1, 2017 at 8:03 am #1289091 | |
pierS-2 |
HI, Unfortunately, on load the product search link to default language and not browser language. I have checked the "Store a language cookie to support language filtering for AJAX" Please let me know. Kind regards |
December 20, 2018 at 12:29 am #3018396 | |
julizarS |
Good morning Bigul, I have an issue with Advanced Custom Fields Multilingual update, I cant update it since I renew my wordpress to 5.1 Can you please open me a new ticket? Thanks New threads created by Bigul and linked to this one are listed below: |
September 26, 2019 at 1:30 pm #4647265 | |
phoenixM |
Hello Pier, |