我将使用哪个重定向规则来重定向olddomain下的所有页面。示例被重定向到newdomain.example?

该网站有一个完全不同的结构,所以我想在旧域下的每个页面都被重定向到新的域索引页。

我认为这将(在旧域。例如基本目录):

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.example/ [R=301]

但如果我导航到olddomain。我被重定向到newdomain.example/somepage。我期待重定向到newdomain。不带页后缀的示例。

我怎么才能不提最后一部分?


当前回答

我作为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>

就像这样简单,这将不会携带从URL到新域的尾随查询。

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule .* https://www.newdomain.com/? [R=301,L]

有各种各样的方法来做到这一点和各种重定向,我将它们列在下面:

301(永久)重定向:永久地将整个网站指向不同的URL。这是最常见的重定向类型,在大多数情况下都很有用。在这个例子中,我们重定向到“example.com”域:

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://example.com/

302(临时)重定向:将整个网站指向不同的临时URL。这对于搜索引擎优化很有用,当你有一个临时的登录页面,并计划在稍后的日期切换回你的主登录页面:

# This allows you to redirect your entire website to any other domain
Redirect 302 / http://example.com/

将index.html重定向到特定的子文件夹:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/

将旧文件重定向到新文件路径:

# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html

重定向到特定的索引页:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

我尝试了user968421的答案和OP的解决方案,但浏览器弹出了结帐页面的安全错误。我不能告诉你确切的原因。

我们的主机(Site Ground)也搞不清楚。

最终的解决方案很接近,但对user968421的答案进行了轻微的调整(边注:与OP不同,我试图重定向到相应的页面,而不仅仅是到主页,所以我维护了从user968421的答案的反向引用[域名后的$1]):

RewriteEngine on
RewriteRule (.*) https://newdomain.com/$1 [R=301,L]

从Host Gator文章推荐的这个htaccess重定向生成器得到了调整(绝望的时间,绝望的措施,amiright?)

可能是这样的:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]