301 Redirects on Apache maintaining URL

I've got a wordpress blog on an apache server that we're migrating to another domain (I've posted a similar question about IIS by the way). What we want to do is to forward to the new domain, but keep the URL intact. Therefore:


forwards to :


What's the best way to do that in .htaccess?
Who is Participating?
Frederic SuneCEO, IT in MIND inc.Commented:
For apache, you need to use htaccess configuration: put this .htaccess file on your old site to redirect to the new one:
# This allows you to redirect your entire website to any other domain
Redirect 301 / http://domainnew.com/
andythecalAuthor Commented:
And will that redirect and keep the URL? It won't just send them to the home page of the site?
Frederic SuneCEO, IT in MIND inc.Commented:
For the specific folder on the old domain, to the new domain:

Redirect 301 /the-best-dogs http://www.domainnew.com/the-best-dogs

Open in new window

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

andythecalAuthor Commented:
Sorry, I probably didn't make it clear.

We have lots of pages on the old domain and we don't want to redirect each one, we want it to keep the URL and just redirect everyone to the new domain, but to the page they wanted.

http://www.domainnew.com/the-best-dogs was an example, but it could be anything after the domain so it's like a wild card.


www.domainold.com/*anything here*


www.domainnew.com/*what was in the URL above*

Sorry, I probably confused the issue!
Frederic SuneCEO, IT in MIND inc.Commented:
Ok so you can use this htaccess on the old website:
Redirect 301 / http://www.domainnew.com/

But the main old domain, www.domainold.com, the root need to be accessible?
you should better use RewriteEngine of apache.
Check http://httpd.apache.org/docs/2.2/rewrite/remapping.html
there is your use case explaned in the "Resource Moved to Another Server" section

The easiest solution would be to alter the DNS record of your OLD domain, to make it a CNAME (alias) for the new Domain, then assuming the new hosting provider permits you to have multiple host names associated with a single virtual host, you can access the new Wordpress instance using either domain name.

If this isn't an option then you'll want a 307, not a 301 redirect, from your old site to the new. A 307 is a temporary redirect that should preserve the existing Hostname in the URL bar, and allow any form data to also be redirected.

.htaccess entry:
RewriteEngine On
RewriteRule  .*   http://www.domainnew.com%{REQUEST_URI} [L]

Open in new window

andythecalAuthor Commented:
When I got my head round it, the original answer did the trick!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.