Permanently Redirect Subfolder Blog to Subdomain on Another Server

Seems this server redirect question may be better suited for serverfault than wordpress stack exchange.

Originally we had a static marketing site at example.com and the wordpress blog hosted at www.joesexample.com/blog.

We are now moving that blog to blog.joesexample.com. However this blog is hosted on a different server (same server name but different WP specific host).

And the original marketing site is still hosted on the original other server.

So presumably on the original host in www.joesexample.com/blog we will empty that folder and add a .htaccess that will need a 301 redirect so anyone going to joesexample.com/blog will be permanently redirected to blog.joesexample.com as well as well as redirecting all posts to the new location. For instance redirecting www.joesexample.com/blog/2014/01/my-post-title/ to blog.joesexample.com/2014/01/my-post-title/

Based on results from Google would the following work:

RewriteEngine ON    
RewriteCond %{REQUEST_URI} ^/blog
RewriteRule ^(.*)$ http://blog.joesexample.com/$1 [R=301,L,P]

Wasn't sure since they are on different servers (ips) if that would matter, I'm thinking it won't since the domain name is the same and also if that rule will only cover the main blog homepage redirect or all the posts, I'm not sure if it will redirect the posts that are in year/month/post title format.

Finally, the reason why I'm asking here first rather than of course simply testing it myself is because it is another person with access to this particular server so I want to be fairly confident it will work before I have them try.

UPDATE - Went ahead and tried having the person use the above htaccess. It all appears to be working except it's not rewriting the URL in the url field.

What I mean is if I go to http://www.joesexample.com/blog it is now properly showing the content from http://blog.joesexample.com however the URL field still shows http://www.joesexample.com/blog. So it sounds like I still need an additional htaccess rule that will change the URL appearance in the URL bar. Any thoughts?


Solution 1:

You are using the [P] flag which causes the request to be handled by mod_proxy.

If you want the browser to be redirected using a HTTP 301 code and therefore display the real URL then your flags should only be [R=301,L].

This might help you:

  • Apache 2.2 documentation about flags
  • Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask
  • Stupid htaccess tricks