.htaccess重写语言子域名

FFi*_*ish 3 subdomain .htaccess mod-rewrite

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

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

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

我甚至无法在/es/文件夹中使用符号链接.

他们回复了我

对于您的托管包有什么问题,我们建议您使用.htaccess和mod_rewrite工具.您可以在子目录中放置.htaccess文件,将es.domain.com网址重写为domain.com/es/anything_else

通过这种方式,访问者或搜索引擎仍将看到

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>
Run Code Online (Sandbox Code Playgroud)

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

Oli*_*ons 6

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

检查您的主机:如果它开头es,则更改您的文档根目录.这是小费:

RewriteCond %{HTTP_HOST} ^es\.mydomain\.com$
# Change the path:
RewriteRule ^/?$ /public/www/index.php
Run Code Online (Sandbox Code Playgroud)

现在,您可以考虑一些更先进的东西:同一网站的多语言.

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]
Run Code Online (Sandbox Code Playgroud)

好的,现在我们有一个环境变量,其中设置了语言.

你这样问了:

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

所以添加这个最终规则:

RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php
Run Code Online (Sandbox Code Playgroud)

总而言之:

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
Run Code Online (Sandbox Code Playgroud)

我没有得到的是:为什么你不能只为所有语言写一次,并做出类似的东西:

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]
Run Code Online (Sandbox Code Playgroud)

所以现在想象一下类型:

  • 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>
Run Code Online (Sandbox Code Playgroud)

[编辑2]

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

  • Original URL => where it goes
  • 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
Run Code Online (Sandbox Code Playgroud)

所以这应该给:

  • Original URL => where it goes
  • 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