简体   繁体   中英

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

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

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)

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

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.

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

Remember that you have to be able to put mod_rewrite rules in your .htaccess files. Having the option of AllowOverride FileInfo for the directory would make sure of that.

Have you read the official documentation about 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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