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

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

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

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

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

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


当前回答

可能是这样的:

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

其他回答

最简单的方法就是:

重定向301 / http://www.newsite.com/

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

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

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] 

下面的答案可能会导致无限重定向循环…

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

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

重定向:

www.newdomain.com

可能是这样的:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://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