我将使用哪个重定向规则来重定向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并将所有其他人员重定向到新域。

其他回答

可能是这样的:

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

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

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

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

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

示例. htaccess

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

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

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

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