简体   繁体   English

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

[英]RewriteCond redirect a domain to another except for one script, do the opposite

1) in general, I want everything on www.ABC.com to be redirected to www.XYZ.com 1)一般来说,我希望将 www.ABC.com 上的所有内容重定向到 www.XYZ.com

2) EXCEPT when it's www.ABC.com/this/123([az]+).html... it must Rewrite (NOT redirect) to... www.ABC.com/that_script.php?var=123 2)除了它是 www.ABC.com/this/123([az]+).html... 它必须重写(不重定向)到... www.ABC.com/that_script.php?var=123

3) Also EXCEPT : when it's www.XYZ.com/this/123([az]+).html... it must go (redirect) to.... www.ABC.com/this/123([az]+).html (so the 2nd rule will apply after that) 3) 也除外:当它是www.XYZ.com/this/123([az ]+).html... 它必须 go(重定向)到.... www.ABC.com/this/123([az ]+).html(所以第二条规则之后适用)

EDIT Both domains parked on the same hosting, so sharing the same.HTACCESS编辑两个域都停在同一主机上,因此共享 same.HTACCESS

EDIT2 Language of the project is PHP项目的EDIT2语言是 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.我用 %{REQUEST_URI} 或 %{SCRIPT_FILENAME} 尝试了各种 RewriteCond,但它从来没有用过,要么说它是一个无限循环,要么根本不接受条件。


EDIT3 In PHP, it would looks like that... EDIT3在 PHP 中,它看起来像这样......

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我想要这个,但是在.HTACCESS

Based on what you have above, it will be something to the effect of:根据您上面的内容,这将产生以下影响:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
ReWriteRule ^/this/([a-z0-9]+).html www.ABC.com/that_script.php?var=$1 [PT,L]
RewriteCond %{HTTP_HOST} www.ABC.com$ [NC]
ReWriteRule ^(.*)$  www.XYZ.com [R=301,L]
</IfModule>

This will do the following -这将执行以下操作 -

1 - Any traffic hitting http://www.ABC.com/this/<Anything made of Numbers and Letters> will pass through to http://www.ABC.com/that_script.php?var=<Anything made of Numbers and Letters> while continuing to say http://www.ABC.com/this/<Anything made of Numbers and Letters> to the visitor. 1 - 任何http://www.ABC.com/this/<Anything made of Numbers and Letters>流量都将传递到http://www.ABC.com/that_script.php?var=<Anything made of Numbers and Letters> ,同时继续对访客说http://www.ABC.com/this/<Anything made of Numbers and Letters>

2 - Any traffic hitting anything other than what is referenced to #1 will be redirected to www.XYZ.com with a HTTP code of 301 (Moved Permanently). 2 - 任何访问除#1 引用之外的任何流量都将重定向到 www.XYZ.com,代码 HTTP 为 301(永久移动)。

Remember that you have to be able to put mod_rewrite rules in your .htaccess files.请记住,您必须能够将 mod_rewrite 规则放入 .htaccess 文件中。 Having the option of AllowOverride FileInfo for the directory would make sure of that.为目录选择AllowOverride FileInfo可以确保这一点。

Have you read the official documentation about mod_rewrite ?您是否阅读过有关mod_rewrite的官方文档? All the information you need is in the manual, there is no secret.您需要的所有信息都在手册中,没有秘密。

RewriteEngine On
RewriteBase /

# Redirect www.xyz.com/this/123([a-z]+).html to www.abc.com/this/123([a-z]+).html.
RewriteCond %{HTTP_HOST} ^www.xyz.com$ [AND]
RewriteCond %{REQUEST_URI} ^/this/123([a-z]+).html$
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=301,L]

# Rewrite 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=123 [L]

# Redirect everything else to www.xyz.com.
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteRule ^(.*)$ http://www.xyz.com/$1 [R=301,L]

Use this code in your .htaccess under DOCUMENT_ROOT:在 DOCUMENT_ROOT 下的 .htaccess 中使用此代码:

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.确认一切正常后,将所有R更改为R=301

Also note that I have used first RewriteRule in a way to avoid 1 extra forward (RewriteRule # 2 above).另请注意,我首先使用 RewriteRule 以避免 1 次额外转发(上面的 RewriteRule #2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM