【发布时间】:2018-03-28 12:59:40
【问题描述】:
我正在努力处理我网站中的语言。
到目前为止,我正在处理我的页面
# Mother page, no child : http://www.example.com/2-news.html
RewriteRule ^([0-9]*)-([a-zA-Z-]*).html$ index.php?id=$1&parent=0 [L]
# Child page : http://www.example.com/2-news_1-news-title.html
RewriteRule ^([0-9]*)-([a-zA-Z-]*)_([0-9]+)-([a-zA-Z0-9-]+).html$ index.php?id=$3&parent=$1 [L]
这样很好用。
现在我想向我的网站添加一些语言,我希望它以这种方式工作:
- 用户到达
http://www.example.com我检查是语言并根据它重定向:- 法语:你留在
http://www.example.com - 英文:你搬到
http://www.example.com/en - 我的数据库中没有任何现有语言:您移至
http://www.example.com/en
- 法语:你留在
那么我的 URL 将如下所示:
-
http://www.example.com/2-news.html到index.php?lang=fr&id=2&parent=0。 -
http://www.example.com/en/2-news.html到index.php?lang=en&id=2&parent=0。
所以http://www.example.com/2-news_1-news-title.html 将是index.php?lang=fr&id=1&parent=2 和http://www.example.com/en/2-news_1-news-title.html 将是index.php?lang=en&id=1&parent=2。
我这样做了
RewriteCond %{HTTP:Accept-Language} ^(fr|en) [NC]
RewriteRule ^$ %1 [L]
RewriteRule ^$ en [L]
RewriteRule ^(fr|en)$ index.php?lang=$1&id=$2&parent=0 [L,NC,QSA]
# Level 1
RewriteRule ^(fr|en)/([0-9]*)-([a-zA-Z-]*).html$ index.php?lang=$1&id=$2&parent=0 [L,NC,QSA]
# Level 2
RewriteRule ^(fr|en)/([0-9]*)-([a-zA-Z-]*)_([0-9]+)-([a-zA-Z0-9-]+).html$ index.php?lang=$1&id=$4&parent=$2 [L]
效果不错,但与我预期的不太一样。对于应该是我网站根目录的法语,如果我之前不添加fr/,其他链接将不起作用。
http://www.example.com/fr/2-news.html 有效,而 http://www.example.com/2-news.html 无效。
任何帮助将不胜感激。
干杯
【问题讨论】:
标签: regex apache .htaccess redirect mod-rewrite