301 Redirect specific pages to new domain?

Hi All,

How would I go about a 301 redirect using my .htaccess file to achieve the following…?

I have Blog 1 (example1.com) that I have moved to Blog 2 (example2.com/blog/)

I want to direct all Blog 1 posts to its corresponding Blog 2 posts like so:

example1.com/blog-post-one/ will forward to[B] example2.com/blog/blog-post-one/

example1.com/another-post/ [/B]will forward to example2.com/blog/another-post/

Is this possible without having to manually add each post redirect to the htaccess file?

Thanks!

-Josh

Josh,

Of course it is possible!

RewriteEngine on
RewriteCond %{HTTP_HOST} example1\\.com [NC]
RewriteRule .? http://example2.com/blog%{REQUEST_URI} [R=301,L]

On the assumption that the two domains are co-located, I’ve checked that the request was made of example1.com (no case because domain names are case insensitive) then redirected EVERYTHING to example2.com’s blog subdirectory.

Regards,

DK

Thanks DK.

Appreciate the code.

Question though: What exactly does “co-located” mean?!

-Josh

Josh,

“co-located” means just that: The two domains are on the same server and IN THE SAME PHYSICAL SPACE, i.e., their requests would be run though the same .htaccess. That is what required the check on the {HTTP_HOST} above.

Regards,

DK

Ahh, I see…

No, these domains are on two different servers.

I didn’t know that was an issue when dealing with a 301 redirect this way.

Is there a way in this situation then?

Thanks.

-Josh

Josh,

Sure, you don’t need the {HTTP_HOST} line. To forward EVERYTHING:

RewriteEngine on
RewriteRule .? http://example2.com/blog%{REQUEST_URI} [R=301,L]

Regards,

DK

OK, great.

I will give it go.

Thank you for explaining that.

-Josh

Hi DK,

I am now in the process of redirecting another blog, this time on the same server.

My current blog posts are located at the example url of:

[B]http://www.example.com/my/blog/article-title-here/[/B]

and I’d like to 301 redirect all posts to this url (on the same domain):

[B]http://www.example.com/article-title-here/[/B]

So according to the code you previously provided, is this now correct:

RewriteEngine on
RewriteRule .? http://www.example.com/%{REQUEST_URI} [R=301,L]

And I would place the code in the .htaccess in the /my/blog/.htaccess location?

Thanks.

-Josh

No, that code is LOOPY (will get stuck in an infinite loop of redirects).

Instead, put this in the .htaccess in the root directory:


RewriteEngine on
RewriteRule ^my/blog/(.+) http://www.example.com/$1 [R=301,L]

If your blog titles can contain only certain characters you should replace (.+) with something more suitable.

Thank you for clearing that up, Scallio.

It works perfectly.

Just to make sure, I put that code in the .htaccess file of the root directory in /my/blog, yes? It worked when I put it there…

Also, what if I already have RewriteEngine On in my htaccess file - do I need to place it again or is that redundant?

Thanks.

No, you only need to have RewriteEngine on in your .htaccess once. It doesn’t break if you have it in there multiple times, but it does make things slower (because of the redundant call(s))

Got it. Thanks a bunch. Thanks a bunch.

(See, redundancy is not better!)