.htaccess 重写语言子域

标签 .htaccess mod-rewrite subdomain

我需要在我的网站上配置子域以获得多语言支持。
我可以设置域以供本地使用,指向一个文件夹,但我的主机不允许我将它们指向我的应用所在的主根目录

/public/es
/public/www/index.php

es.domain.com需要指向/public/www/index.php

我什至不能在 /es/ 文件夹中使用符号链接(symbolic link)。

他们回复了我

For what concerns your hosting pack, we could suggest you to use the .htaccess and mod_rewrite tools. You could place an .htaccess file in your subdirectories to rewrite the es.domain.com URL to domain.com/es/anything_else

In this way the visitor or search engine will still see

es.domain.com/anything as address.

我试过这是 /es/ 文件夹,但得到一个 403

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^es.domain.com/?$   ^domain.com/es_ES?$ [L,R=301]   
</IfModule>

EDIT 我的网站设置为使用 es.domain.com 子域更改语言。

最佳答案

这是您可能想要做的,以便将来能够处理许多子语言。

检查您的主机:如果它以 es 开头,则更改您的 文档根目录。 这是提示:

RewriteCond %{HTTP_HOST} ^es\.mydomain\.com$
# Change the path:
RewriteRule ^/?$ /public/www/index.php

现在您可能会想到一些更高级的东西:同一网站的多语言。

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]

# Now check if the LANGUAGE is empty (= doesn't) exists
RewriteCond %{ENV:LANGUAGE} ^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]

好的,现在我们有一个设置语言的环境变量。

你要求这个:

es.domain.com needs to point to /public/www/index.php

所以添加这条最终规则:

RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php

总而言之:

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
# Now check if the LANGUAGE is empty (= doesn't exist)
RewriteCond %{ENV:LANGUAGE} ^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]
# Change the root folder of the spanish language:
RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php

我不明白的是:你为什么不能只为所有语言编写一次,然后编写如下内容:

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
# Now check if the LANGUAGE is empty (= doesn't exist)
RewriteCond %{ENV:LANGUAGE} ^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]

# WHATEVER THE LANGUAGE ADD IT TO THE URI:
RewriteRule (.*) $1?language=%{ENV:LANGUAGE} [QSA]

所以现在想象一下有人打字:

  • http://es.mydomain.com/
  • http://us.mydomain.com/
  • http://fr.mydomain.com/
  • http://pt.mydomain.com/

所有都指向同一段代码,您只需在 Php 文件中处理它:查看 $_GET[' language'] 变量并读取好的“翻译”文件。

这只是一个建议,可帮助您减少工作,让非常健壮应用程序!


希望这会有所帮助!


[编辑 1]

这是您可以放入 .htaccess 文件的最后内容:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
    RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
    RewriteCond %{ENV:LANGUAGE} ^$
    RewriteRule (.*) - [QSA,E=LANGUAGE:es]
    RewriteRule (.*) $1?language=%{ENV:LANGUAGE} [QSA]
</IfModule>

[编辑 2]

我的最新规则是这样做的:

  • 原始网址 => 去向
  • http://pt.domain.com/=> http://pt.domain.com/?language=es
  • http://pt.domain.com/aa.php => http://pt.domain.com/aa.php?language=es
  • http://es.domain.com/=> http://es.domain.com/?language=es
  • http://es.domain.com/aa.php => http://es.domain.com/aa.php?language=es
  • http://domain.com/=> http://domain.com/?language=es
  • http://domain.com/bb.php => http://domain.com/bb.php?language=es

根本不会改变路径

这个改变路径:

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
RewriteCond %{ENV:LANGUAGE} ^$
RewriteRule (.*) - [QSA,E=LANGUAGE:es]
RewriteCond %{ENV:LANGUAGE} ^es$
RewriteRule ^/?$ /public/www/index.php

所以这应该给出:

  • 原始网址 => 去向
  • http://pt.domain.com/=> http://pt.domain.com/
  • http://pt.domain.com/aa.php => http://pt.domain.com/aa.php
  • http://es.domain.com/=>/public/www/index.php
  • http://es.domain.com/aa.php => http://es.domain.com/aa.php
  • http://domain.com/=>/public/www/index.php
  • http://domain.com/bb.php => http://domain.com/bb.php

关于.htaccess 重写语言子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9531002/

相关文章:

.htaccess - htaccess 将子域指向文件夹

.htaccess - 将域和子域重定向到 HTTPS

Apache RewriteMap 用于防止直接访问文件

php - 如何使用 .htaccess 重写 URL 而不会影响 $_GET 数据

ruby-on-rails - Rails 4 动态子域

regex - 删除文件扩展名 php 除了一个文件

php - 使用 .htaccess 访问远程数据

php - 处理 URL 参数 - 以斜杠分隔的名称值对

apache - 如何在 Ubuntu 上为 Apache2 配置子域?

php - 相同的服务器,不同的域需要不同的 session