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

时间:2009-12-22 10:45:15

标签: .htaccess mod-rewrite

我会使用哪种重定向规则重定向olddomain.example下的所有网页,以重定向到newdomain.example

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

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

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

但如果我导航到olddomain.example/somepage,我会被重定向到newdomain.example/somepage。我期待仅重定向到newdomain.example而没有页面后缀。

如何保留最后一部分?

19 个答案:

答案 0 :(得分:408)

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

此处,此网址会将网址上的所有内容重定向到新域网址上的完全相同的副本:

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

答案 1 :(得分:178)

可能是这样的:

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

答案 2 :(得分:76)

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

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

答案 3 :(得分:17)

我已经将我的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 ^(.*)$ http://YOURDOMAIN.example/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

答案 4 :(得分:13)

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

这对我有用

答案 5 :(得分:10)

有多种方法可以执行此操作以及各种重定向,我在下面列出了它们:

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

# 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

答案 6 :(得分:9)

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

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

答案 7 :(得分:6)

如果您要将旧网站重定向到的新域位于不同的主机上,则只需使用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] 

答案 8 :(得分:5)

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

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

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

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

答案 9 :(得分:3)

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

答案 10 :(得分:2)

如果您想重定向以完成没有参数或页面的新的不同站点(域),请使用:

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

答案 11 :(得分:2)

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

我们的主人(Site Ground)也无法解决这个问题。

最终的解决方案很接近,但对用户968421的回答略有调整(旁注:与OP不同,我试图重定向到相应的页面,而不仅仅是主页,所以我保留了后面的引用[{1]来自user968421的答案之后的域名]:

$1

通过htaccess redirect generator推荐的这个Host Gator article进行了调整(绝望的时候,绝望的措施,amiright?)。

答案 12 :(得分:2)

从可用性的角度来看,如果你也发送带有请求的路径(即你现在拥有的东西)并让你的新网站处理它会更好:

  

您搜索了“/ products”。

     

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

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

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

答案 13 :(得分:2)

这是旧版本的apache(以及mod_rewrite)中的错误,如果路径前缀发生更改,则会将路径前缀附加到重写的路径。 See here

我认为它已在apache2 V2.2.12中修复,你需要使用一个特殊的标志,当我找到它时,我会在这里添加,(我认为它是无路径的NP)

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

答案 14 :(得分:1)

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

示例.htaccess

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

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

答案 15 :(得分:1)

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

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

答案 16 :(得分:0)

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

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

答案 17 :(得分:0)

最简单的方法其实就是:

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

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

答案 18 :(得分:0)

这可能正常工作

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