htaccess的重写语言子域 [英] .htaccess rewrite for language subdomains
问题描述
我要在我的网站的多语言支持配置的子域。
我可以设置域本地使用指向一个文件夹,但我的主机不允许我给他们指出的主要根源在哪里我的应用程序
/公/ 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~~V在这样的访客或搜索引擎仍然会看到
es.domain.com/anything的地址。
我想这是 / ES /
文件夹,却得到了一个403
< IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /
重写规则^ es.domain.com /?$ ^ domain.com / es_ES?$ [L,R = 301]
< / IfModule>
修改我的网站设置为更改使用 es.domain.com
子域的语言。
下面是你想做的事,要能够处理在未来的很多子语言是什么。
检查您的主机:它是否与 ES开始
,然后更改您的文档根目录。
这里的技巧:
的RewriteCond%{HTTP_HOST} ^ ES \ .mydomain \ .COM $
#更改路径:
重写规则^ /?$ /public/www/index.php
而现在更多的东西一点点前进,你可能会想:多语言的同一网站。
的RewriteCond%{HTTP_HOST} ^(我们| FR | PT)。\ MYDOMAIN \ .COM $
#创建一个环境变量要记住的语言:
(。*)重写规则 - [QSA,E =语言:%1]
#现在检查如果语言是空的(=不)存在
的RewriteCond%{ENV:语言} ^ $
#如果是这样,创建默认语言(= ES):
(。*)重写规则 - [QSA,E =语言:西班牙文]
好了,现在我们已经在那里的语言设置环境变量。
您问这样的:
es.domain.com
需要指向/public/www/index.php
所以添加此最终规则:
的RewriteCond%{ENV:语言} ^ ES $
#更改根文件夹:
重写规则^ /?$ /public/www/index.php
所以,这一切的一切:
的RewriteCond%{HTTP_HOST} ^(我们| FR | PT)。\ MYDOMAIN \ .COM $
#创建一个环境变量要记住的语言:
(。*)重写规则 - [QSA,E =语言:%1]
#现在检查如果语言是空的(=不存在)
的RewriteCond%{ENV:语言} ^ $
#如果是这样,创建默认语言(= ES):
(。*)重写规则 - [QSA,E =语言:西班牙文]
#更改西班牙语的根文件夹:
的RewriteCond%{ENV:语言} ^ ES $
#更改根文件夹:
重写规则^ /?$ /public/www/index.php
我不明白的是:为什么你就不能写的一旦所有语言,并是这样的:
的RewriteCond%{HTTP_HOST} ^(我们| FR | PT)。\ MYDOMAIN \ .COM $
#创建一个环境变量要记住的语言:
(。*)重写规则 - [QSA,E =语言:%1]
#现在检查如果语言是空的(=不存在)
的RewriteCond%{ENV:语言} ^ $
#如果是这样,创建默认语言(= ES):
(。*)重写规则 - [QSA,E =语言:西班牙文]
#语言添加任何IT的URI:
重写规则$ 1语言=%(*){ENV:语言} [QSA]
所以,现在想象有人类型:
-
http://es.mydomain.com/
-
http://us.mydomain.com/
-
http://fr.mydomain.com/
-
http://pt.mydomain.com/
所有点为同一块code ,然后你只需要处理它在你的腓
文件:窥视< STRONG> $ _ GET ['语言']
变量并宣读了良好的翻译的文件。
这仅仅是一个建议,帮助您制作的较少的工作的的的非常强大的应用程序!
希望这有助于!
下面是最终的东西,你可以把你的的.htaccess
文件:
&LT; IfModule mod_rewrite.c&GT;
RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{HTTP_HOST} ^(我们| FR | PT)。\ MYDOMAIN \ .COM $
(。*)重写规则 - [QSA,E =语言:%1]
的RewriteCond%{ENV:语言} ^ $
(。*)重写规则 - [QSA,E =语言:西班牙文]
重写规则$ 1语言=%(*){ENV:语言} [QSA]
&LT; / IfModule&GT;
我的最新的的规则做到这一点:
-
原始地址
=>,哪里就有奇迹
-
http://pt.domain.com/ =&GT; http://pt.domain.com/?language=es
-
http://pt.domain.com/aa.php =&GT; http://pt.domain.com/aa.php?language=es
-
http://es.domain.com/ =&GT; http://es.domain.com/?language=es
-
http://es.domain.com/aa.php =&GT; http://es.domain.com/aa.php?language=es
-
http://domain.com/ =&GT; http://domain.com/?language=es
-
http://domain.com/bb.php =&GT; http://domain.com/bb.php?language=es
这并没有改变路径的所有
这一个改变了的路径:
的RewriteCond%{HTTP_HOST} ^(我们| FR | PT)。\ MYDOMAIN \ .COM $
(。*)重写规则 - [QSA,E =语言:%1]
的RewriteCond%{ENV:语言} ^ $
(。*)重写规则 - [QSA,E =语言:西班牙文]
的RewriteCond%{ENV:语言} ^ ES $
重写规则^ /?$ /public/www/index.php
所以,这应该给:
-
原始地址
=>,哪里就有奇迹
-
http://pt.domain.com/ =&GT; http://pt.domain.com/
-
http://pt.domain.com/aa.php =&GT; http://pt.domain.com/aa.php
-
http://es.domain.com/ =&GT; /public/www/index.php
-
http://es.domain.com/aa.php =&GT; http://es.domain.com/aa.php
-
http://domain.com/ =&GT; /public/www/index.php
-
http://domain.com/bb.php =&GT; http://domain.com/bb.php
I need to configure subdomains on my website for multilanguage support.
I can set up the domains for local use pointing to a folder but my host does not allow me to point them to the main root where my app is
/public/es
/public/www/index.php
es.domain.com
needs to point to /public/www/index.php
I can't even use symlinks in the /es/
folder.
They replied to me
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_elseIn this way the visitor or search engine will still see
es.domain.com/anything as address.
I tried this is the /es/
folder but get a 403
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^es.domain.com/?$ ^domain.com/es_ES?$ [L,R=301]
</IfModule>
EDIT My site is set up to change the language using the es.domain.com
subdomain.
Here's what you may want to do, to be able to handle in the future many sub languages.
Check your host: if it begins with es
, then change your document root.
Here's the tip:
RewriteCond %{HTTP_HOST} ^es\.mydomain\.com$
# Change the path:
RewriteRule ^/?$ /public/www/index.php
And now something a little bit more advanced you may think about: multilanguage for the same website.
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]
Ok now we have an environment variable where the language is set.
You asked for this:
es.domain.com
needs to point to/public/www/index.php
So add this final rule:
RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php
So all in all:
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
What I don't get is: why can't you just write once for all languages, and make something like:
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]
So now imagine someone types:
http://es.mydomain.com/
http://us.mydomain.com/
http://fr.mydomain.com/
http://pt.mydomain.com/
All point to the same piece of code and you just have to handle it in your Php
file: look into the $_GET['language']
variable and read the good "translation" file.
This is just an advice to help you making less work for a very robust application!
Hope this helps!
[Edit 1]
Here's the final stuff you may put into your .htaccess
file:
<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>
[Edit 2]
My latest rules do this:
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
It doesn't change the path at all.
This one changes the path:
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
So this should give:
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
这篇关于htaccess的重写语言子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!