JCary
March 15, 2010, 4:19pm
1
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
dklynn
March 15, 2010, 6:42pm
2
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
JCary
March 16, 2010, 3:21pm
3
Thanks DK.
Appreciate the code.
Question though: What exactly does “co-located” mean?!
-Josh
dklynn
March 16, 2010, 9:14pm
4
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
JCary
March 16, 2010, 9:47pm
5
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
dklynn
March 17, 2010, 1:58am
6
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
JCary
March 17, 2010, 1:49pm
7
OK, great.
I will give it go.
Thank you for explaining that.
-Josh
JCary
February 8, 2011, 8:51pm
8
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
rpkamp
February 9, 2011, 9:41am
9
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.
JCary
February 9, 2011, 5:10pm
10
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.
rpkamp
February 9, 2011, 8:08pm
11
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))
JCary
February 9, 2011, 8:35pm
12
Got it. Thanks a bunch. Thanks a bunch.
(See, redundancy is not better!)