htaccess将语言特定的域重定向到新域

时间:2018-10-12 04:08:16

标签: apache .htaccess url-redirection

我有两个旧域http://example1.com英语)和http://example2.com法语),希望它们重定向到我的新域,例如为:

  

http://example1.com => http://newdomain.com/en/

     

http://example2.com => http://newdomain.com/fr/

我还要确保永久链接在重定向后也保持不变。

例如http://example1.com/test.html => http://newdomain.com/en/test.html

我的代码:

以下代码无法维护永久链接,我也不知道如何在检查中添加法语域。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteRule (.*)$ http://www.newdomain.com/en/$1 [R=301,L]
</IfModule>

2 个答案:

答案 0 :(得分:1)

以下方法应该起作用:

RewriteEngine On

RewriteCond %{HTTP_HOST} example1.com$
RewriteRule (.*) http://www.example.com/en/$1 [R=301,L]

RewriteCond %{HTTP_HOST} example2.com$
RewriteRule (.*) http://www.example.com/fr/$1 [R=301,L]

关于保持永久链接,我认为您不能在这里完成,必须在新域的.htaccess中进行。

您使用什么翻译?它接受查询参数还是能够自行解析uri?

答案 1 :(得分:0)

像这样:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?example1\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/en%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^(?:www\.)?example2\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/fr%{REQUEST_URI} [R=301,L,NE]

请确保将这些规则作为您的最重要的规则,然后再使用其他规则,并在新的浏览器中进行测试以避免旧的浏览器缓存。