|.htaccess to redirect /directory/ to subdomain|
I had to move my blog from
How do I make all the files in the /blog directory redirect to the new subdomain, where they are not in a subdirectory.
Hi Andrea, and welcome to WebmasterWorld!
Because we have a limited number of contributors and lots of requests for answers here, we ask that you post your best-effort code and ask very specific questions.
It is important for the proper functioning of your site and for your ability to maintain it, that you completely understand any server configuration code (like code in .htaccess) that you put on your server. Time spent learning about .htaccess modules and their directives is a very wise investment, since one single typo or small logical error can take your site site off-line -- or silently ruin your search engine rankings over time.
This previous thread [webmasterworld.com], along with the resources cited in our Apache Forum Charter and the examples and tutorials in our Apache Forum Library, will help you get started.
This is what I have tried:
But I can't get it to stop adding the /blog/ directory name to the end of the URL when it redirects it. The old URL was in a /blog/ subdirectory, but now the root of the blog is just blog.example.com
If anyone could provide help, I would appreciate it, I have read a bunch of the tutorials available however this situation doesn't seem to be covered.
[edited by: jdMorgan at 11:33 pm (utc) on Apr 15, 2010]
[edit reason] Please use example.com only. [/edit]
You need a back-reference, and "blog/" must be excluded from that back-reference.
Assuming that this code goes into your top-level .htaccess file, you'll need something like:
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [R=301,L]
Here, the requested URL-path must start with "blog/" to invoke this rule, otherwise it is ignored. Anything following "blog/" is matched by the "match anything, everything, or nothing" subpattern of ".*" and since that subpattern is enclosed in parentheses, whatever matches this subpattern in the requested URL-path will be copied into mod_rewrite's local variable $1. We then redirect to the subomain, appending the contents of $1, and using a 301-Moved Permanently redirect status response. Because we wish mod_rewrite to take no more action after this rule is invoked, we end processing with an [L] flag.
See the resources cited in our Apache Forum Charter for more information.
I also had to add another rule that would rewrite any requests specifically for blog.html, which was the blog home page on the original setup, whereas the new home page is just blog.example.com/ - so I got that to work - see I'm learning already!
... and that is our purpose, here.
I'm glad that you got it sorted. :)