duanlaican1849 2012-04-07 13:35
浏览 50
已采纳

RewriteCond将域重定向到另一个域,除了一个脚本,执行相反的操作

1) in general, I want everything on www.ABC.com to be redirected to www.XYZ.com

2) EXCEPT when it's www.ABC.com/this/123([a-z]+).html ... it must Rewrite (NOT redirect) to ... www.ABC.com/that_script.php?var=123

3) Also EXCEPT: when it's www.XYZ.com/this/123([a-z]+).html ... it must go (redirect) to .... www.ABC.com/this/123([a-z]+).html (so the 2nd rule will apply after that)

EDIT Both domains parked on the same hosting, so sharing the same HTACCESS

EDIT2 Language of the project is PHP


I tried various RewriteCond with %{REQUEST_URI} or %{SCRIPT_FILENAME} but it never works, either saying it's an infinite loop or simply don't take the condition at all.


EDIT3 In PHP, it would looks like that...

if( FALSE !== strstr($_SERVER['HTTP_HOST'], 'ABC.com') && FALSE !== strstr($_SERVER['SCRIPT_FILENAME'], 'that_script') ) {
    header("Location: http://www.XYZ.com".$_SERVER['REQUEST_URI'],TRUE,301);
}
if( FALSE !== strstr($_SERVER['HTTP_HOST'], 'XYZ.com') && FALSE === strstr($_SERVER['SCRIPT_FILENAME'], 'that_script') ) {
    header("Location: http://www.ABC.com".$_SERVER['REQUEST_URI'],TRUE,301);
}

I want this, but in HTACCESS

  • 写回答

3条回答 默认 最新

  • dscw1223 2012-04-11 13:44
    关注

    Use this code in your .htaccess under DOCUMENT_ROOT:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # Redirect www.xyz.com/this/123([a-z]+).html to www.abc.com/that_script.php?var=123
    RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$ [NC]
    RewriteRule ^this/(123)[a-z]+\.html$ http://www.abc.com/that_script.php?var=$1 [R,L,NC]
    
    # Forward www.abc.com/this/123([a-z]+).html to www.abc.com/that_script.php?var=123
    RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$ [NC]
    RewriteRule ^this/(123)[a-z]+\.html$ that_script.php?var=$1 [L,QSA,NC]
    
    # Redirect abc.com to www.xyz.com
    RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$ [NC]
    RewriteRule ^ http://www.xyz.com%{REQUEST_URI} [R,L]
    

    Change all R to R=301 once you have verified that it is all working fine.

    Also note that I have used first RewriteRule in a way to avoid 1 extra forward (RewriteRule # 2 above).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?