Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have found a number of different solutions of what should be written in the .htaccess file but for some reason they don't work perfectly.

I would like to redirect all content from old site to go to the new site - www.oldsite.com/first/article to redirect to www.newsite.com/first/article

and so on. I have heard that this is the correct way to redirect permanently when moving a site from one domain to another in respect to SEO, so that search engines do not doublicate the content and penalize for it.

So if anyone can provide working code, that would be great. Explanations on why that would work and what things in it mean would be in the awesomeness category.

thanx a bunch.

PS, i have mod_rewrite enabled already.

share|improve this question

2 Answers 2

I highly suggest John Gruber's Using .htaccess Redirection to Standardize Web Server Addresses. The "GETTING RID OF ‘WWW’ PREFIXES" section gives a straightforward example of the simple rewrite you need along with a clear explanation.

You can substitute www\.oldsite\.com for www\.daringfireball\.net and www.newsite.com for daringfireball.net, respectively, in his example, giving you the following:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.oldsite\.com$
RewriteRule (.*) http://www.newsite.com/$1 [R=Permanent]

Or, in the "REDIRECTING FROM ‘.COM’ TO ‘.NET’" section, he explains how to use mod_alias to do exactly what you're asking for, as follows:

RedirectMatch permanent (.*) http://www.newsite.com$1

His article is a great overview of both methods and gives you the knowledge to pick whichever works best (obviously mod_alias is the shorter implementation, but mod_rewrite tends to be the more flexible in the long run, in my opinion). He also links to Dave Child's excellent mod_rewrite Cheat Sheet.

share|improve this answer
    
hi, i used those. the main domain redirects. but if i type www.oldsite.com/anything/ does not take me to www.newsite.com –  mgPePe Jul 21 '11 at 12:12
    
So www.oldsite.com -> www.newsite.com works, but not www.oldsite.com/anything -> www.newsite.com/anything? Which method did you use, mod_rewrite (RewriteCond + ReWriteRule) or mod_alias (RedirectMatch)? Is there anything else in your .htaccess file? –  morgant Jul 21 '11 at 15:50

Create a .htaccess file with the code given below. It will ensure that all the directories and pages on your old domain will correctly redirect to your new domain. The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymLinks
# RewriteEngine on - You have this enabled already, so this is not required.
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Please REPLACE www.newdomain.com in the above code with your actual domain name.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.