.htaccess 301从旧域重定向到新域,而页面和URL的结构相同

时间:2022-08-23 10:41:42

I want to redirect http://olddomain.com to http://newdomain.com for my all urls..keeping the page on new domain same. What i mean to say is URLs such as below

我想将http://olddomain.com重定向到http://newdomain.com以获取我的所有网址。在新域名上保持相同的页面。我的意思是URL如下

http://olddomain.com/home/category/page.html
http://olddomain.com/home/mybook/page2.html
http://olddomain.com/login

should be 301 redirect to the new newdomain but same pages, like below

http://newdomain.com/home/category/page.html
http://newdomain.com/home/mybook/page2.html
http://newdomain.com/login

this is what i have in my .htaccess currently

这就是我目前在.htaccess中所拥有的

RewriteEngine on
RewriteCond $1 !^(index\.php|img|public|robots\.txt) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

Please help me to do this cleanly and exlpain things in details since i am new in this.

因为我是新手,所以请帮我干净利落地详细解释。

also does someone know how much time search engines might take to move away from the references of my olddomain? i mean the old-domain urls in search queries should be replaced by new-domain urls... n old domain should go away from search engines.

也有人知道搜索引擎可能需要花多少时间才能远离我的olddomain的引用?我的意思是搜索查询中的旧域网址应该被新域网址替换...旧域名应该远离搜索引擎。

4 个解决方案

#1


26  

Add following code at the beginning of .htaccess -

在.htaccess的开头添加以下代码 -

    RewriteEngine On

    # Redirect Entire Site to New Domain
    RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC]
    RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

#2


7  

Much simpler:

更简单:

Redirect 301 / http://newdomain.com/

Replace your .htaccess file with that one line OR if you have access to it, put it in the apache conf file(s) for your old domain (I place it following the DocumentRoot directive).

将.htaccess文件替换为该行,或者如果您有权访问它,请将其放在旧域的apache conf文件中(我将其置于DocumentRoot指令之后)。

See Redirecting and Remapping with mod_rewrite for more info.

有关详细信息,请参阅使用mod_rewrite重定向和重新映射。

#3


1  

I usually add the following codes in .htaccess in the old website

我通常在旧网站的.htaccess中添加以下代码

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

This above code will redirect all links to new domain, you don't have to do anything, each link and images redirect to new domain link. Beside this we have to tell Google when your site moves

上面的代码会将所有链接重定向到新域,您不必做任何事情,每个链接和图像都会重定向到新的域链接。除此之外,我们必须在您的网站移动时告诉Google

If you've moved your site to a new domain, you can use the Change of address tool to tell Google about your new URL. We'll update our index to reflect your new URL. Changes will stay in effect for 180 days, by which time we'll have crawled and indexed the pages at your new URL. Here is the link for this https://support.google.com/webmasters/answer/83106?hl=en

如果您已将网站移至新域名,则可以使用地址更改工具向Google报告您的新网址。我们将更新我们的索引以反映您的新网址。更改将在180天内有效,届时我们将抓取您的新网址并将其编入索引。以下是此https://support.google.com/webmasters/answer/83106?hl=zh-CN的链接

I have done this for 2 to 3 sites without losing seo as well. It does work. Thanks

我已经为2到3个站点做了这个,同时也没有丢失seo。它确实有效。谢谢

#4


0  

Tried to do

试过去做

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

But did not worked the way it should, was just trying to point my localhost to newdomain.com.

但是没有按照它应该的方式工作,只是试图将我的localhost指向newdomain.com。

But when I hit localhost still it does not point.

但是,当我点击localhost仍然没有指向。

#1


26  

Add following code at the beginning of .htaccess -

在.htaccess的开头添加以下代码 -

    RewriteEngine On

    # Redirect Entire Site to New Domain
    RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC]
    RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

#2


7  

Much simpler:

更简单:

Redirect 301 / http://newdomain.com/

Replace your .htaccess file with that one line OR if you have access to it, put it in the apache conf file(s) for your old domain (I place it following the DocumentRoot directive).

将.htaccess文件替换为该行,或者如果您有权访问它,请将其放在旧域的apache conf文件中(我将其置于DocumentRoot指令之后)。

See Redirecting and Remapping with mod_rewrite for more info.

有关详细信息,请参阅使用mod_rewrite重定向和重新映射。

#3


1  

I usually add the following codes in .htaccess in the old website

我通常在旧网站的.htaccess中添加以下代码

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

This above code will redirect all links to new domain, you don't have to do anything, each link and images redirect to new domain link. Beside this we have to tell Google when your site moves

上面的代码会将所有链接重定向到新域,您不必做任何事情,每个链接和图像都会重定向到新的域链接。除此之外,我们必须在您的网站移动时告诉Google

If you've moved your site to a new domain, you can use the Change of address tool to tell Google about your new URL. We'll update our index to reflect your new URL. Changes will stay in effect for 180 days, by which time we'll have crawled and indexed the pages at your new URL. Here is the link for this https://support.google.com/webmasters/answer/83106?hl=en

如果您已将网站移至新域名,则可以使用地址更改工具向Google报告您的新网址。我们将更新我们的索引以反映您的新网址。更改将在180天内有效,届时我们将抓取您的新网址并将其编入索引。以下是此https://support.google.com/webmasters/answer/83106?hl=zh-CN的链接

I have done this for 2 to 3 sites without losing seo as well. It does work. Thanks

我已经为2到3个站点做了这个,同时也没有丢失seo。它确实有效。谢谢

#4


0  

Tried to do

试过去做

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

But did not worked the way it should, was just trying to point my localhost to newdomain.com.

但是没有按照它应该的方式工作,只是试图将我的localhost指向newdomain.com。

But when I hit localhost still it does not point.

但是,当我点击localhost仍然没有指向。