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

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

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

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

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

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


当前回答

这是apache旧版本中的一个错误(因此mod_rewrite),如果路径被更改,路径前缀会附加到重写的路径上。在这里看到的

我认为它在apache2 V2.2.12中被修复了,有一个你需要使用的特殊标志,当我找到它时,我会在这里添加它,(我认为它是NP for No Path)

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

其他回答

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

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

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

重定向301 /Old-Location/ http://subdomain.yourdomain.com

只是为了澄清,在删除托管重定向这是在路上,我原来的解决方案也起作用:

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

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

你搜索了“/products”。 不幸的是,这一页已经消失了。您是否愿意访问“/new_products”?

(更好的是,自动地做这件事。)

对于一个更大的网站来说,这显然是大量的编码和启发式,但在我看来,这在用户满意度方面是值得的(当你精心保存的梦想产品书签只是把你引向newdomain.com的首页时,这是令人沮丧的)。

使用条件重定向选项-FollowSymLinks和AllowOverride -Options被主机禁用,如果一些本地文件也应该服务:

示例. htaccess

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

这个例子将提供本地index.html并将所有其他人员重定向到新域。