Welcome to WebmasterWorld Guest from 54.226.147.190

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess to redirect /directory/ to subdomain

     

Andrea Wasik

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

5+ Year Member



I had to move my blog from

www.me.com/blog

to

blog.me.com

How do I make all the files in the /blog directory redirect to the new subdomain, where they are not in a subdirectory.

Thanks!

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Jim

Andrea Wasik

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

5+ Year Member



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.

Thanks!

[edited by: jdMorgan at 11:33 pm (utc) on Apr 15, 2010]
[edit reason] Please use example.com only. [/edit]

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Jim

Andrea Wasik

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

5+ Year Member



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!

Andrea

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



... and that is our purpose, here.

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

Jim
 

Featured Threads

Hot Threads This Week

Hot Threads This Month