繁体   English   中英

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

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

1)一般来说,我希望将 www.ABC.com 上的所有内容重定向到 www.XYZ.com

2)除了它是 www.ABC.com/this/123([az]+).html... 它必须重写(不重定向)到... www.ABC.com/that_script.php?var=123

3) 也除外:当它是www.XYZ.com/this/123([az ]+).html... 它必须 go(重定向)到.... www.ABC.com/this/123([az ]+).html(所以第二条规则之后适用)

编辑两个域都停在同一主机上,因此共享 same.HTACCESS

项目的EDIT2语言是 PHP


我用 %{REQUEST_URI} 或 %{SCRIPT_FILENAME} 尝试了各种 RewriteCond,但它从来没有用过,要么说它是一个无限循环,要么根本不接受条件。


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);
}

我想要这个,但是在.HTACCESS

根据您上面的内容,这将产生以下影响:

<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>

这将执行以下操作 -

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 - 任何访问除#1 引用之外的任何流量都将重定向到 www.XYZ.com,代码 HTTP 为 301(永久移动)。

请记住,您必须能够将 mod_rewrite 规则放入 .htaccess 文件中。 为目录选择AllowOverride FileInfo可以确保这一点。

您是否阅读过有关mod_rewrite的官方文档? 您需要的所有信息都在手册中,没有秘密。

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]

在 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]

确认一切正常后,将所有R更改为R=301

另请注意,我首先使用 RewriteRule 以避免 1 次额外转发(上面的 RewriteRule #2)。

暂无
暂无

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

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