简体   繁体   中英

.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. 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

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.

Add following code at the beginning of .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).

See Redirecting and Remapping with mod_rewrite for more info.

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

#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

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

I have done this for 2 to 3 sites without losing seo as well. 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.

But when I hit localhost still it does not point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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