Say I own "example.com" and I want everyone typing in "example.com" to be directed to "blog.example.com"

However, if someone types in "example.com/directory", I don't want it to be redirected to "blog.example.com/directory"

Is this achievable by a set of rewrite conditions and rules?

2 answers

Doug 1095
2
points
This was chosen as the best answer
RewriteEngine on
RewriteRule ^$ http://blog.example.com/ [R=301,L]

(Found here)

Answered over 7 years ago by Doug
  • That was surprisingly simple. wow. Thanks! Andy Zhang over 7 years ago
  • Yeah, I tried several more complicated schemes before finding that one. Happy New Year! Doug over 7 years ago
Doug 1095
0
points

It probably can be done in .htaccess; I can look into it if necessary. But a much easier method is to use PHP. I've done this exact same thing on a site I worked on recently.

Create a file called index.php in the root directory of example.com with only this in it:

<?php 
header("Location: http://blog.example.com/",TRUE,301);

"301" means "Moved Permanently". Use "307" for a "Temporary Redirect". Whether you use 301 or 307 will affect how search engines and browsers treat that link in the future.

Answered over 7 years ago by Doug
  • Yes, that's exactly how it is set up right now =] However, that requires the PHP engine to process this tiny thing every time instead of Apache taking care of it as soon as the visitor reaches the server. It does add up once lots of visitors arrive from digg and the sorts. A .htaccess method would be much appreciated! Andy Zhang over 7 years ago
  • Out of curiosity, have you noticed slowdowns in the past because of this? Doug over 7 years ago