将"文件夹组合为GET参数"和"子域作为GET参数"

时间:2014-09-15 22:27:08

标签: regex apache .htaccess mod-rewrite

我有一个网站将域之后的路径重写为page GET参数。

##REWRITING DIRECTORIES TO GET PARAMETERS
RewriteBase /
#Ignore all real directories (do not rewrite them)
RewriteCond %{REQUEST_FILENAME} !-d
#Also do not rewrite real files
RewriteCond %{REQUEST_FILENAME} !-f
#For everything else, index.php should fetch the proper content
RewriteRule ^([^/]*)$ index.php?page=$1 [QSA,L]

##This means:
#example.com/help
#~becomes~
#example.com?page=help

该网站使用多种语言,到目前为止,我一直在使用Cookie来设置和记住用户的语言。虽然用户的便利性是有争议的,但对于SEO来说这绝对不方便。

我需要将[a-z]{2}\.mydomain\.xx重写为index.php?lang=$1,以便用户始终在en.domain.com上。有一些例子可以做到这一点,但是我仍然对重写引擎如何工作感到困惑,而且我不知道如何将新规则与旧规则结合起来:

##Language rewrite
#Copypasted. Didn't understand
RewriteCond %{HTTP_HOST} ^([a-z]{1,2})\.domain\.xx
RewriteRule ([a-z]{1,2})\.domain\.xx index.php?lang=$1 [QSA,L]

如何en.domain.com/help转入index.php?page=help&lang=en

1 个答案:

答案 0 :(得分:1)

  

如何在index.php中输入en.domain.com/help?page = help& lang = zh

您可以使用:

RewriteEngine On
RewriteBase /

#Ignore all real directories (do not rewrite them)
RewriteCond %{REQUEST_FILENAME} !-d
#Also do not rewrite real files
RewriteCond %{REQUEST_FILENAME} !-f
#For everything else, index.php should fetch the proper content
RewriteCond %{HTTP_HOST} ^([a-z]{1,2})\.domain\.xx$ [NC]
RewriteRule ^([^/]+)/?$ index.php?lang=%1&page=$2 [QSA,L]

参考:Apache mod_rewrite Introduction