繁体   English   中英

.htaccess 将所有页面重定向到新域

[英].htaccess redirect all pages to new domain

我将使用哪个重定向规则将olddomain.example下的所有页面重定向到newdomain.example

该站点具有完全不同的结构,因此我希望将旧域下的每个页面都重定向到新域索引页面

我认为这会做(在 olddomain.example 基本目录下):

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

但是,如果我导航到olddomain.example/somepage我会被重定向到newdomain.example/somepage 我期望只重定向到没有页面后缀的newdomain.example

我如何保留最后一部分?

以下答案可能会导致无限重定向循环......

在这里,这个将 URL 上的域名之后的所有内容重定向到新域 URL 上的完全相同的副本:

RewriteEngine on 
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
www.example.net/somepage.html?var=foo

重定向到:

www.newdomain.com/somepage.html?var=foo

可能是这样的:

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

只是为了澄清,在删除了阻碍的托管重定向后,我的原始解决方案也有效:

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

我在我的 Wordpress 博客中使用了 .htaccess。 它将http://www.blah.example/asadhttp://blah.example/asadhttp://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>
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

这对我有用

有多种方法可以做到这一点和各种重定向,我在下面列出了它们:

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

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

302(临时)重定向:将整个站点指向不同的临时 URL。 当您有一个临时登录页面并计划在以后切换回主登录页面时,这对于 SEO 目的非常有用:

# 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

如果您想从某个位置重定向到子域,您可以使用:

Redirect 301 /Old-Location/ http://subdomain.yourdomain.com

如果您将旧站点重定向到的新域位于不同的主机上,您可以简单地使用Redirect

Redirect 301 / http://newdomain.com

这会将所有请求从olddomain重定向到newdomain

如果您的 newdomain 和 olddomain 都在同一主机上,则Redirect指令将不起作用或可能导致Redirect loop ,在这种情况下,您需要使用mod-rewrite根据请求的主机标头进行重定向。

RewriteEngine on


RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [NE,L,R] 

我的声誉不允许我对答案发表评论,但我只想指出这里评分最高的答案有一个错误:

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

在 $1 之前应该有一个斜线,所以

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

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

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

我的 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();
}

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

我们的主机(Site Ground)也无法弄清楚。

最终的解决方案很接近,但对 user968421 的答案稍作调整(旁注:与 OP 不同,我试图重定向到相应的页面,而不仅仅是主页,因此我保留了反向引用 [域后的$1 ] 来自user968421 的回答):

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

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

这是旧版本 apache(以及 mod_rewrite)中的一个错误,其中路径前缀被附加到重写的路径(如果它被更改)。 看这里

我认为它已在 apache2 V2.2.12 中修复,您需要使用一个特殊标志,当我找到它时我会在此处添加它,(我认为它是 No Path 的 NP)

RewriteRule ^(.*)$ http://newdomain.com/ [??]

从可用性的角度来看,如果您还发送请求的路径(即您目前拥有的路径)并让您的新站点处理它会更好:

您搜索了“/products”。

不幸的是,这个页面已经消失了。 您想访问“/new_products”吗?

(更好,仍然是自动执行此操作。)

对于一个较大的网站来说,这显然是很多编码和启发式方法,但在我看来,这会在用户满意度方面得到回报(当您精心保存的梦想产品书签只是将您带到 newdomain.com 的首页时,这令人沮丧。)

如果要重定向以完成没有参数或页面的新的其他站点(域),请使用以下命令:

RewriteEngine On
RewriteRule / http://new-domain.com/$1? [R=301]

尝试使用此方法将所有内容重定向到主页新域,它对我有用:

RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ "https\:\/\/newdomain\.com\/" [R=301,L]

如果也应该提供一些本地文件,请使用由 Hoster 禁用的Options -FollowSymLinksAllowOverride -Options Options -FollowSymLinks条件重定向:

示例 .htaccess

# Redirect everything except index.html to http://foo
<FilesMatch "(?<!index\.html)$">
    Redirect 301 / http://foo/
</FilesMatch>

此示例将提供本地 index.html 并将所有其他员工重定向到新域。

以前的答案对我不起作用。

我使用了这个代码。 如果您使用的是 OSX,请确保使用正确的格式。

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

最简单的方法实际上只是:

Redirect 301 / http://www.newsite.com/

来源: https : //github.com/phanan/htaccess#redirect-an-entire-site

这可能是正常工作

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>

只需换出域并保持请求 URI 不变:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
</IfModule>

%{REQUEST_URI}是请求路径的别名。

R=301是 HTTP 301 永久移动

L表示最后一条规则,不再考虑任何条件。

要同时调整请求 URI,请执行以下操作:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^(.*)$ https://example.com/subfolder/$1 [R=301,QSA,L]
</IfModule>

^(.*)$捕获请求路径中从开始到结束的所有内容。

$1输出第一个捕获的组

QSA表示附加的查询字符串(例如 ?foo=bar)

暂无
暂无

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

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