【问题标题】:mod-rewrite domainA to domainB with parameters or individually redirectmod-rewrite 域到带有参数的域或单独重定向
【发布时间】:2014-01-30 03:05:40
【问题描述】:

我有一个具有旧 url 结构的旧域,并希望将其重定向 (301) 到具有新 url 结构的另一个域。我有必须单独转换的网址:

http://domainA.com/ => http://ru.domainB.com/ http://domainA.com/?fieldA=abc => http://ru.domainB.com/xxx/zzz http://domainA.com/?fieldA=www&fieldB=aaa => http://ru.domainB.com/uuu/ooo/ppp

另一个网址具有相同的值:

http://domainA.com/?fieldC=abc&fieldD=4 => http://ru.domainB.com/abc/xxx/4 http://domainA.com/?fieldC=abc&fieldD=5 => http://ru.domainB.com/abc/xxx/5 http://domainA.com/?fieldC=abc&fieldD=6 => http://ru.domainB.com/abc/xxx/6 http://domainA.com/?fieldC=def&fieldD=4 => http://ru.domainB.com/def/xxx/4 http://domainA.com/?fieldC=def&fieldD=5 => http://ru.domainB.com/def/xxx/5 http://domainA.com/?fieldC=def&fieldD=6 => http://ru.domainB.com/def/xxx/6

左侧网址可以在“?”之前有可选的“index.php”或“万维网”。在域名之前。有人可以在这里帮我翻译这 4 个链接吗?请用 mod_rewrite(apache)?

我的帐户有多个域名用于一个网站空间(通配符子域)。 apache conf中的伪设置:

服务器别名 *.domainA.com 服务器别名 *.domainB.com 服务器别名 *.domainC.com 服务器别名 *.domainD.com

编辑:这对我有帮助。

RewriteCond %{HTTP_HOST} ^(www\.|)domainA\.com RewriteCond %{QUERY_STRING} 字段A=xxx) RewriteRule ^(.*)$ http://ru.domainB.com/? [R=301,L] RewriteCond %{HTTP_HOST} ^(www\.|)domainA\.com RewriteCond %{QUERY_STRING} 字段A=abc RewriteRule ^(.*)$ http://ru.domainB.com/abc/bbb/? [R=301,L] RewriteCond %{HTTP_HOST} ^(www\.|)domainA\.com RewriteCond %{QUERY_STRING} fieldA=abc&fieldB=(\d+) RewriteRule ^(.*)$ http://ru.domainB.com/abc/%1? [R=301,L]

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

    在你的 .htaccess 文件中只做一个重定向 301 怎么样?

    redirect 301 http://domainA.com/ http://ru.domainB.com/
    

    更新:如果您需要所有 4 个 URL,并且它们像您说的那样非常具体。然后试试这个。这应该适用于所有 4 个 URL。

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (^|&)fieldA=abc(&|$) [NC]
    RewriteRule ^(.*)$ http://ru.domainB.com/xxx/zzz? [R=301,L,NC]
    
    RewriteCond %{QUERY_STRING} (^|&)fieldA=www&fieldB=aaa(&|$) [NC]
    RewriteRule ^(.*)$ http://ru.domainB.com/uuu/ooo/ppp? [R=301,L,NC]
    
    RewriteCond %{QUERY_STRING} (^|&)fieldC=abc&fieldD=4(&|$) [NC]
    RewriteRule ^(.*)$ http://ru.domainB.com/abc/xxx/4? [R=301,L,NC]
    
    RewriteRule ^(.*)$ http://ru.domainB.com/$1 [R=301,L,NC]
    

    更新 2:

    然后试试这个新的 URL。这将匹配 fieldC 和 fieldD 与 ABCDEF

    RewriteCond %{QUERY_STRING} ^fieldC=(.*)&fieldD=(.*) [NC]
    RewriteRule ^(.*)$ http://ru.domainB.com/%1/xxx/%2 [R=301,L,NC]
    

    【讨论】:

    • 感谢您的回答和提示,但这不是我需要的 100%。我已经使用新链接编辑了我的文本。我只需要域A。在您的示例中,此网络空间的每个域都将使用您的“RewriteRule”。
    • 好的,你到底想要 doaminA 什么?你希望它总是有 www 还是有关系?
    • @user706420 我更新了答案以包含您添加的新 URL。这应该进入您位于domainA 根目录中的.htaccess 文件。那么这些规则只适用于domainA
    • 我会接受你的回答,因为它可以帮助我解决这个问题。在您的示例中是不必要的代码。在“QUERY_STRING”中,您写道:(^|&) 和 (&|$)。您可以删除它和它的工作相同。参数中的整数值应该是 (\d+) 而不是你的 (.*) 等... :) 谢谢!
    • 也许,但他们奏效了。 :) 是的,我不知道它是否总是一个数字。但是我可以使用 \w+。不管怎样,很高兴你把它解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2013-05-25
    • 2015-12-24
    • 2014-02-07
    相关资源
    最近更新 更多