将旧域反向链接重定向到新域

时间:2014-04-23 17:31:36

标签: regex apache .htaccess mod-rewrite redirect

我即将在一个新域名上发布我的某个网站的更新版本,我想知道如何制作它以便旧网址指向我的新网站,因为它有很多有价值的反向链接。< / p>

我已经阅读过如何在.htaccess中重定向301,但是由于我的一些url结构发生了变化,我需要更高级的东西。请参阅以下示例。

olddomain.com
newdomain.com 
- These will do just fine with the 301 redirect

olddomain.com/author/username
newdomain.com/user/username
- Different URL structure but I want the old urls to redirect towards the new url

olddomain.com/add
newdomain.com/submit
- Different page name for the same functionaly, would like to have these redirect correctly as well

我不知道如何创建最后两个重定向,所以如果有人能指出我正确的方向,这将是伟大的! :)

2 个答案:

答案 0 :(得分:2)

将此代码放入DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^$ http://newdomain.com/ [R=301,L]

RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^author/([^/]+)/?$ http://newdomain.com/user/$1 [R=301,L]

RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^add/?$ http://newdomain.com/submit [R=301,L]

答案 1 :(得分:1)

您是否尝试过使用RedirectMatch

olddomain.com文档根目录中的htaccess文件中(必须与您的newdomain.com根目录不同):

RedirectMatch 301 ^/author/(.*)$ http://newdomain.com/user/$1
RedirectMatch 301 ^/add(.*)$ http://newdomain.com/submit$1
Redirect 301 / http://newdomain.com/
相关问题