htaccess - 将旧域重定向到新域,但有异常

问题描述

htaccess - 将旧域重定向到新域,但有异常

我有一个旧域:www.olddomain.com,带有 URL 文件:1.PHP、2.PHP 和 3.PHP 我想将 www.olddomain.com 重定向www.newdomain.com,但重定向后 3.PHP自动重定向/重命名为 4.PHP,即:

www.olddomain.com/3.PHP => www.newdomain.com/4.PHP

其余的应该是一样的:

www.olddomain.com/1.PHP => www.newdomain.com/1.PHP
www.olddomain.com/2.PHP => www.newdomain.com/2.PHP

RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
Redirect /3.PHP /4.PHP[N]
RewriteRule (.*)$ https://www.newdomain.com/$1 [R=301,L]

当然它不能按我想要的方式工作,请帮忙。

解决方法

在通用重定向规则之前有特定的重定向:

RewriteEngine on

# redirect 3.php
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^3\.php$ https://www.newdomain.com/4.php [L,NC,R=301]

# redirect everything else
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [R=301,L,NE]
,

试试:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^\/3\.php [NC] #usually use NC if using Windows
RewriteRule ^ https://www.newdomain.com/4.php [R=301,L]

RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !\/3\.php [NC] #usually use NC if using Windows
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]
#or if it adds two slashes use https://www.newdomain.com$1