homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

.htaccess to redirect /directory/ to subdomain
Andrea Wasik

 11:54 am on Apr 15, 2010 (gmt 0)

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.




 1:30 pm on Apr 15, 2010 (gmt 0)

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.


Andrea Wasik

 6:19 pm on Apr 15, 2010 (gmt 0)

Hi Jim,

This is what I have tried:

RewriteEngine ON
RewriteRule (.*)\.*$
http://blog.example.com/ [R=301,L]

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]


 12:18 am on Apr 16, 2010 (gmt 0)

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:

RewriteEngine on
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.


Andrea Wasik

 11:18 am on Apr 16, 2010 (gmt 0)

Thanks Jim!

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!



 4:17 pm on Apr 16, 2010 (gmt 0)

... and that is our purpose, here.

I'm glad that you got it sorted. :)


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved