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

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

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

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

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

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


当前回答

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

示例. htaccess

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

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

其他回答

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

这对我很有效

我在我的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>

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

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

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

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

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

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

对于一个更大的网站来说,这显然是大量的编码和启发式,但在我看来,这在用户满意度方面是值得的(当你精心保存的梦想产品书签只是把你引向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