htaccess 带有子目录的多语言站点,默认为 301

编程入门 行业动态 更新时间:2024-10-09 08:28:56
本文介绍了htaccess 带有子目录的多语言站点,默认为 301的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

www.domain/en/ www.domain/sw/ www.domain/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:

dev.domain/devname/projectname/

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

dev.domain/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

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

dev.domain/home/webserver_dir/devname/projectname/en/

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

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

dev.domain/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

... 但现在它重定向到 dev.domain/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 dev.domain/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

本文发布于:2023-11-02 07:11:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子目录   多语言   默认为   站点   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!