我将使用哪个重定向规则来重定向olddomain下的所有页面。示例被重定向到newdomain.example?
该网站有一个完全不同的结构,所以我想在旧域下的每个页面都被重定向到新的域索引页。
我认为这将(在旧域。例如基本目录):
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.example/ [R=301]
但如果我导航到olddomain。我被重定向到newdomain.example/somepage。我期待重定向到newdomain。不带页后缀的示例。
我怎么才能不提最后一部分?
如果您要将旧站点重定向到的新域位于不同的主机上,您可以简单地使用重定向
Redirect 301 / http://newdomain.com
这将把所有来自olddomain的请求重定向到newdomain。
重定向指令将不起作用或可能导致重定向循环,如果你的newdomain和olddomain都在同一个主机上,在这种情况下,你需要使用mod-rewrite重定向基于请求的主机头。
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [NE,L,R]
我作为SymLinks的解决方案在我的服务器上不起作用,所以我在我的PHP中使用了一个If。
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$redirect = str_replace("www.", "", curPageURL());
$remove_http_root = str_replace('http://', '', $redirect);
$split_url_array = explode('/', $remove_http_root );
if($split_url_array[0] == "olddomain.com"){
header("Location: http://www.newdomain.com/$split_url_array[1]");
die();
}
我在我的Wordpress博客中用的是。htaccess。它将http://www.blah.example/asad, http://blah.example/asad, http://www.blah.example2/asad等转换为http://blah.example/asad
感谢所有其他的答案,我想出了这个。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^YOURDOMAIN\.example$ [NC]
RewriteRule ^(.*)$ https://YOURDOMAIN.example/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>