【问题标题】:Permanent redirect 301 with /?lang=de带有 /?lang=de 的永久重定向 301
【发布时间】:2017-04-03 21:12:26
【问题描述】:

我需要将一个多语言网站重定向到一个新域,并将为其使用 .htaccess 文件。我将所有旧页面重定向到新域上各自的新页面。

旧网址

Englisch
    Main page: www.olddomain.com
    Sub pages: www.olddomain.com/bike-rental

Spanish
    Main: www.olddomain.com/?lang=es
    Sub pages: www.olddomain.com/bike-rental?lang=es

German
    Main: www.olddomain.com/?lang=de
    Sub pages: www.olddomain.com/bike-rental?lang=de

新网址

Englisch
    Main page: www.new-domain.com
    Sub pages: www.new-domain.com/bike-rental

Spanish
    Main: www.new-domain.com/es
    Sub pages: www.new-domain.com/es/alquiler-de-bicis
German
    Main: www.new-domain.com/de
    Sub pages: www.new-domain.com/de/fahrradvermietung

我意识到它不适用于 .htaac​​ces 文件中的正常重定向 如:

    Redirect 301 /?lang=de http://www.new-domain.com/de/
    Redirect 301 /bike-rental?lang=de http://www.new-domain.com/de/fahrradvermietung

知道如何将旧网址重定向到新网址吗?

谢谢

【问题讨论】:

  • 你是如何实现重定向的?通过代码或网络服务器?提及更多细节

标签: php .htaccess redirect url-redirection http-status-code-301


【解决方案1】:

你可以在 htaccess 中做这样的事情:

RewriteCond %{REQUEST_URI} !^/bike-rental$
RewriteCond %{QUERY_STRING} ^lang=(.*)$ 
RewriteRule ^(.*)/ /bike-rental?lang=%1 [L,R=301]

(已测试并添加条件以避免循环重写)

【讨论】:

  • 抱歉,这个链接如何指向我需要重定向的新域: www.old-domain.com/?lang=de 到 www.new-domain.com/de 或子页面: www.old-domain.com/bike-rental/?lang=de 到 www.new-domain.com/de/fahrradvermietung
  • 总是可以添加 RewriteRule ^(.*)/ http//www.new-domain.com/bike-rental?lang=%1 [L,R=301] 和子页面你只需要一点点改变
【解决方案2】:

查看我之前的消息: 由于php添加了这个功能:

$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale; // returns "en_US"
$localeshort = substr($locale, -2);
echo $localeshort; // returns "US"

在您的 .htacces 中使用

RewriteEngine on

RewriteRule ^(.*)$ /index.php [R=permanent,L]

现在它将被重定向到开始页面。 这是我知道的唯一解决方案。

【讨论】:

    【解决方案3】:

    或使用:this tool 创建重定向,重复此操作,直到您拥有所有语言。 IMAGE

    【讨论】:

    • ...谢谢,但我需要 301 重定向到新域...而且这个工具肯定不会将 url 重定向到新域...
    【解决方案4】:

    从 php 5.3.0 开始添加了这个变量:

    $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
    echo $locale; // returns "en_US"
    $localeshort = substr($locale, -2);
    echo $localeshort; // returns "US"
    

    如果你使用这个,你可以使用类似的东西进行重定向:

    Header("Location: index.php?lang=".$localeshort);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2013-07-17
    • 1970-01-01
    • 2012-05-31
    • 2015-08-11
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多