Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 330
asm234 Avatar asked Oct 21 '12 19:10

asm234


People also ask

Does a 301 redirect change the URL?

A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL. A 301 redirect passes all ranking power from the old URL to the new URL, and is most commonly used when a page has been permanently moved or removed from a website.

What is a 301 .htaccess redirect?

A 301 Permanent Redirect permanently redirects one URL to another. You set up a 301 redirect using . htaccess to send visitors to a new URL and tell search engines that a page has moved so that the new page can be properly indexed. Some common uses of a 301 redirect with .


2 Answers

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]
like image 84
Parag Meshram Avatar answered Sep 18 '22 07:09

Parag Meshram


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.

like image 26
user12345 Avatar answered Sep 22 '22 07:09

user12345