简体   繁体   English

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

[英].htaccess 301 redirect from old domain to new domain while structure of pages and url are same

I want to redirect http://olddomain.com to http://newdomain.com for my all urls..keeping the page on new domain same. 我想将所有网址的http://olddomain.com重定向到http://newdomain.com ,以使新域上的页面保持不变。 What i mean to say is URLs such as below 我的意思是说如下网址

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. 我的意思是搜索查询中的旧域名网址应替换为新域名网址... n旧域名应远离搜索引擎。

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]

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重定向和重新映射

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. 如果您已将网站移至新域,则可以使用“更改地址”工具将新网址告知Google。 We'll update our index to reflect your new URL. 我们将更新索引以反映您的新URL。 Changes will stay in effect for 180 days, by which time we'll have crawled and indexed the pages at your new URL. 更改将保留180天,届时我们将对您的新URL上的页面进行爬网并编制索引。 Here is the link for this https://support.google.com/webmasters/answer/83106?hl=en 这是此https://support.google.com/webmasters/answer/83106?hl=zh_CN的链接

I have done this for 2 to 3 sites without losing seo as well. 我已经在2到3个网站上做到了这一点,而且还没有失去seo。 It does work. 确实有效。 Thanks 谢谢

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. 但是没有按照应有的方式工作,只是试图将我的本地主机指向newdomain.com。

But when I hit localhost still it does not point. 但是当我点击本地主机时,它仍然没有指向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM