[英]htaccess rewrite to support multilingual site with dropdown selector. How to fix this?
This is my structure: 这是我的结构:
in my page example.com I have multiple languages. 在我的页面example.com中我有多种语言。 In the index page, I have also a dropdown menu that you can change your language. 在索引页面中,我还有一个下拉菜单,您可以更改您的语言。 This dropdown appears in other pages too. 此下拉列表也出现在其他页面中。
This is my htaccess for the root file 这是我对根文件的htaccess
RewriteEngine On
RewriteRule ^(en|pl|cz|hu)?$ index.php?lang=$1 [NC,L]
so, i wrote a code that if the domain is example.com/en
it will load the English translations. 所以,我写了一个代码,如果域是example.com/en
它将加载英文翻译。
My problem is on the other pages I can not change the languages and stay on the same page. 我的问题是在其他页面上我无法更改语言并保持在同一页面上。
So when i am on www.example.com/en
and click on link.php
( example.com/link.php
) I can keep the preffered language this through Session
所以,当我在www.example.com/en
并点击link.php
( example.com/link.php
)时,我可以通过Session
保留link.php
语言
But when I am on example.com/link.php
and change the language, the result is not correct (the result is an output of some PHP code that need fixed and i believe it does not suit here) 但是当我在example.com/link.php
并更改语言时,结果不正确(结果是一些需要修复的PHP代码的输出,我相信它不适合这里)
What I thought is to include the /en/
(or any other language in the urls, like www.example.com/en/link.php
我想的是包含/en/
(或网址中的任何其他语言,例如www.example.com/en/link.php
But this gives me a 404 error. 但这给了我404错误。 I believe is because of the htaccess file and that I do not have any folder named en
etc. 我相信是因为htaccess文件,我没有任何名为en
等的文件夹。
How can I solve this? 我怎么解决这个问题? Is it a htaccess thing? 它是一个htaccess的东西?
What I can think as a logic but not know how to implement is like 我可以认为逻辑但不知道如何实现就像
when the domain contains (en|pl|cz|hu)
followed by /***
do it through the ****.php?lang=$1
当域包含(en|pl|cz|hu)
后跟/***
通过****.php?lang=$1
Try this out : 试试这个:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|pl|cz|hu)/(.*)?$ $2.php?lang=$1 [NC,L]
and you can store you lang variable in session, like following : 你可以在会话中存储lang变量,如下所示:
// link.php
if (isset($_GET['lang']) {
$_SESSION['lang'] = $_GET['lang'];
}
$lang = $_SESSION['lang'];
EDITED: **Now htaccess will work only for .php files. 编辑:**现在htaccess只适用于.php文件。 ** **
RewriteEngine On RewriteRule ^(en|pl|cz|hu)/([a-zA-Z0-9-/]+).php$ $2.php?lang=$1 [NC,L]
Now your link must be www.domain.com/en/index www.domain.com/en/link www.domain.com/pl/link
RewriteEngine On RewriteRule ^(en|pl|cz|hu)/([a-zA-Z0-9-/]+).php$ $2.php?lang=$1 [NC,L]
现在你的链接必须是www.domain.com/en/index www.domain.com/en/link www.domain.com/pl/link
If you have questions, please ask. 如果您有任何疑问,请询问。
You don't need to use .htaccess
at all for this. 你根本不需要使用.htaccess
。 You can just have the form $_POST
back to the page its on. 你可以将表单$_POST
返回到它所在的页面。
if ($_POST['language']) {
$_SESSION[KEY]['Language'] = $_POST['language'];
}
Then depending on the variable within the session, defines what language to use. 然后根据会话中的变量,定义要使用的语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.