htaccess 带有子目录的多语言站点,默认为 301 [英] htaccess multi language site with sub directories, and default 301

查看:27
本文介绍了htaccess 带有子目录的多语言站点,默认为 301的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置我的 htaccess 以允许使用子目录方法的多种语言时遇到一些问题,例如:

I am having some issues setting up my htaccess to allow multiple languages utilising the sub directory method eg:

http://www.domain.com/en/
http://www.domain.com/sw/
http://www.domain.com/ie/

另外让事情复杂化的是,该项目目前还没有上线,它在开发服务器上.例如,我目前正在访问该项目:

Also to complicate things, the project isn't currently live, its on a dev server. For example, I am currently accessing the project at:

http://dev.domain.com/devname/projectname/

我希望以上内容自动 301 重定向到:

http://dev.domain.com/devname/projectname/en/

这是我的 htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine on

# ----------------------------------------------------------------------
# MULTI LANGUAGE SUB DIRECTORY
# ----------------------------------------------------------------------

RewriteCond %{REQUEST_URI} !^/(en|sw)/
RewriteRule ^(.*)$ en/$1 [R=301,L]

# ----------------------------------------------------------------------
# Rewrite rules
# ----------------------------------------------------------------------

## CASE STUDIES ##
RewriteRule ^casestudies/([^/.]+).html$ index.php?controller=contents&method=viewCasestudy&link=$1 [L,QSA]

## PRODUCTS ##
RewriteRule ^products/([^/.]+).html$ index.php?controller=contents&method=viewProduct&link=$1 [L,QSA]

RewriteRule ^([a-z{2}]+)(/)?$ index.php?controller=contents&method=viewHome&lang=$1 [L,QSA] # Default load
RewriteRule ^(/)?$ index.php?controller=contents&method=viewHome [L,QSA] # Default load

以上内容实际上会重定向到:

http://dev.domain.com/home/webserver_dir/devname/projectname/en/

..如果我使用 RewriteBase,它似乎只是转到...

..and if I use RewriteBase it seems to just goto...

http://dev.domain.com/en/

所以我的问题是:我如何让语言 URL 相对于其在我的开发服务器上的目录正常工作,然后理想情况下在没有任何环境特定规则的情况下上线时工作.

So my question: How do I get the language URLs working correctly relative to the directory its in on my dev server, and then ideally will work when it goes live without any environment specific rules.

额外问题:我是否需要在所有后续重写规则前面添加 ([az{2}]+) 位,或者我是否可以捕获所有将影响所有进一步规则的内容?

Bonus question: Do I need to add the ([a-z{2}]+) bit in front of all my subsequent rewrite rules or can I have a catch all that will effect all further rules?

我已按照建议将其缩减为以下...

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteBase /devname/projectname/

RewriteCond %{REQUEST_URI} !^/(en|sw)(/|$) [NC]
RewriteRule ^(.*)$ en/$1 [R=301,L]
RewriteRule ^([a-z]{2})/?$ index.php?controller=contents&method=viewHome&lang=$1 [NC,L,QSA] # Default load

... 但现在它重定向到 http://dev.domain.com/devname/projectname/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/,有什么想法吗?

... but now its redirecting to http://dev.domain.com/devname/projectname/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/, any ideas?

推荐答案

你试过下面链接的答案了吗?它应该做你想要达到的目标.

Have you tried the answer in the following link? It should do what you're trying to achieve.

htaccess 规则多语言的无限重定向循环

RewriteEngine On
RewriteBase /   

# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ en/ [R=301,L]

# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$  $1/ [R=301,L]

# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$  $2?lang=$1&%{query_STRING} [L]

这篇关于htaccess 带有子目录的多语言站点,默认为 301的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆