2010-08-19 144 views
27

我想使用htaccess文件将我的所有旧域请求重定向到我的新域。以下是我正在使用的内容,但如果页面不在新网站上,则不起作用。例如旧网站上的google索引about.htm,但在新网站上它不存在。我希望它能够在所有情况下都能发挥作用。我知道这对于seo来说并不理想,但我不想要任何404s。有什么建议么?htaccess将所有页面重定向到单页

Redirect 301/http://www.thenewdomain.com/ 
+1

可能重复[301的.htaccess重定向旧域中的所有网页上的新域单页](http://stackoverflow.com/questions/15057146/htaccess-301-redirect-all-pages-on-old-domain-to-a-single-page-on-new-domain) – mattdm 2014-07-10 03:04:59

回答

20

您是否试图让访问者访问old.com/about.htm以访问new.com/about.htm?如果是这样,你可以在.htaccess与mod_rewrite的规则做到这一点:

RewriteEngine on

RewriteRule ^(.*)$ http://www.thenewdomain.com/$1 [R=permanent,L]

+0

不,我只是想让old.com/about.htm去old.com和那么如果可能的话删除/about.htm,这样它就会进入old.com – 2010-08-19 15:11:54

+2

只要删除'$ 1'就可以了! – 2013-11-18 01:39:36

11

这将引导一切从旧主机到新主机的根:

RewriteEngine on 
RewriteCond %{http_host} ^www.old.com [NC,OR] 
RewriteCond %{http_host} ^old.com [NC] 
RewriteRule ^(.*)$ http://www.thenewdomain.org/ [R=301,NC,L] 
+1

仅供参考:最后一行的[NC]标志是无用的。 – 2012-11-20 16:46:18

+0

怎么样,重定向所有,除''/ admin'之外?所以可以联系管理小组? – 2015-03-03 16:58:12

+0

@FranciscoCorralesMorales - 要做到这一点,您可以添加以下行: RewriteCond%{REQUEST_URI}!/ admin/$ 进入RewriteCond之前的RewriteRule行。 – 2016-07-14 20:15:26

49

如果您的目标是将所有页面重定向到单个维护页面(如标题可能也会提示),请使用:

RewriteEngine on 
RewriteCond %{REQUEST_URI} !/maintenance.php$ 
RewriteCond %{REMOTE_HOST} !^000\.000\.000\.000 
RewriteRule $ /maintenance.php [R=302,L] 

其中000 000 000应替换为your ip adress

来源:

http://www.techiecorner.com/97/redirect-to-maintenance-page-during-upgrade-using-htaccess/

+0

怎么样,重定向所有,除''/ admin'之外?所以可以联系管理小组? – 2015-03-03 16:58:03

+0

此方法将阻止维护页面上使用的所有资源。尝试在第二行后添加:'RewriteCond%{REQUEST_URI}!\。(jpe?g?| png | gif | js | css | woff | ttf | icico] [NC]' – 2017-10-06 21:06:05

3
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC] 
RewriteRule ^(.*)$ "http://www.thenewdomain.com/" [R=301,L] 
相关问题