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
points
RewriteEngine on
RewriteRule ^$ http://blog.example.com/ [R=301,L]
- That was surprisingly simple. wow. Thanks!
- Yeah, I tried several more complicated schemes before finding that one. Happy New Year!
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.
- 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!
- Out of curiosity, have you noticed slowdowns in the past because of this?
