apache - .htaccess - 将域/lang/重定向到每种语言的子域

标签 apache .htaccess redirect mod-rewrite url-rewriting

我已经为此奋斗了数周,但无法找到解决方案,我必须在多语言网站的 .htaccess 中进行重定向,其所有页面都遵循相同的结构,但具有子域每种语言。以下方式对于 http 作为 https 有效:

domain.es/es/allpages  to  es.subdomain.com/allpages
domain.es/pt/allpages  to  pt.subdomain.com/allpages
domain.es/de/allpages  to  de.subdomain.com/allpages
domain.es/fr/allpages  to  fr.subdomain.com/allpages
domain.es/en/allpages  to  eu.subdomain.com/allpages

有什么想法吗? 我已经尝试过以下但不确定是否正确:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) https://es.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/pt/(.*)$
RewriteRule ^(.*) https://pt.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/fr/(.*)$
RewriteRule ^(.*) https://fr.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/de/(.*)$
RewriteRule ^(.*) https://de.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/en/(.*)$
RewriteRule ^(.*) https://eu.subdomain.com/%1 [R=301,NC]

非常感谢!!!

最佳答案

根据您显示的示例,您可以尝试以下操作吗?请确保在测试您的网址之前清除浏览器缓存。

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.es$ [NC]
RewriteRule ^(es|pt|de|fr|en)/(.*)/?$ http://$1.subdomain.com/$2 [NE,R=301,NC,L]

关于apache - .htaccess - 将域/lang/重定向到每种语言的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65778994/

相关文章:

java - 通过 J_security_check

regex - 如何匹配.htaccess中的问号

php - ELB 后面的 Symfony2 重定向到 http 而不是 https

php - Codeigniter 3 - 如何从 URL 中删除函数名称

php - 拒绝从浏览器访问公共(public) php 但允许应用程序使用

apache - activemq 未启动

apache - 特定 HTTP 重定向帮助 - "Request exceeded the limit of 10 internal redirects"

带有 VirtualHost 代理的 Apache 2.4 缓存

redirect - nginx 重定向(如果不是我的网站)

Python 子进程 - 将 stdout/err 重定向到两个地方