Forum Moderators: phranque

Message Too Old, No Replies

domain.foo/subdomain/blog/#### -> subdomain.domain.foo/blog/####

mod_rewrite help needed so I don't break things

         

compooter

4:40 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



I have a site that has many little things that may or may not be sensitive to breakage. Here is the scenario, I need help on two fronts (one general and one specific).

[ mod_rewrite ]

(www.)domain.foo/sub/blog/someID# -> sub.domain.foo/blog/someID#

If someone could help with two methods, one that would be subdomain-specific (say if the request were 'sub') and one that would apply site-wide to any subdirectory->subdomain.

jdMorgan, anyone? Thanks!

jdMorgan

9:43 pm on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We've covered this subject previously:

[webmasterworld.com...]

Jim

compooter

9:47 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



Hehe, yeah I read that one... but this is actually the /opposite/ of that request. I'll just try searching the forum a little harder. It's gotta be buried in here somewhere. Fuggeddaboutit.

compooter

9:49 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



Ah wait - I think everything was there and I just wasn't hearing you correctly. Thanks for the swift kick!

compooter

12:57 am on Aug 14, 2004 (gmt 0)

10+ Year Member



After an hour or two of reading, searching, & testing I haven't been able to come up with a solution to what has got to be a simple problem. Here's the scenario:

Site used to be run at:

h*tp://domain.com/d/

with several subdirectories below that, such as:

h*tp://domain.com/d/photo/
h*tp://domain.com/d/blog/
etc...

Subdomain has since been created & is fully operational at (same subdirectories still apply):

h*tp://d.domain.com/

Since the old links are not dead and Google, Yahoo, etc have a record of the links, I'd like to 301 rewrite every request for h*tp://domain.com/d/foo/bar/ OR h*tp://www.domain.com/d/foo/bar/ (regardless of URI request) and have it elegantly slide the d in front of as a subdomain, thus h*tp://d.domain.com/foo/bar/

I'm sure it's been asked many times before, but since this is not a question about /creating/ a subdomain - just one regarding a R=301, I can't seem to solve it (also because I'm doing this as a favor for a friend and since my host has automatic subdomain resolving, I'm finding it hard to test). Thanks for any help you've got for me.

gergoe

1:44 am on Aug 14, 2004 (gmt 0)

10+ Year Member



The rewriting in this case is pretty simple, but you need to be careful, where you put the htaccess file, and also that you don't create loops with your rule.
I think the most ideal solution is to put the rules in the root of the domain.com domain, and put a RewriteCond before the RewriteRule (which rewrites the domain.com/d/*** requests to d.domain.com/***), which should test the HTTP_HOST variable against the domain.com text. By doing so the rewriting happens only if the hostname equals to domain.com.

Without the RewriteCond you would end up in a dead loop possibly causing some 500 http errors.

Recommended literarure:
URL Rewriting Guide [httpd.apache.org]
mod_rewrite documentation [httpd.apache.org]

compooter

3:17 am on Aug 14, 2004 (gmt 0)

10+ Year Member



Yeah, I've already got that far and have read thru those documents dozens of times now. The problem is not the strategy to take, but the actual implementation.

jdMorgan

4:43 am on Aug 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please check our forum charter [webmasterworld.com]...

Jim

compooter

6:47 am on Aug 14, 2004 (gmt 0)

10+ Year Member



Ok, I sense your tone. Thanks.

compooter

11:36 pm on Aug 15, 2004 (gmt 0)

10+ Year Member



Well, I solved it on my own and I'll just record it here in case anyone needs it in the future.

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.foo$
Rewriterule ^d/(.*) http://d.domain.foo/$1 [R=301,L]

jdMorgan

11:56 pm on Aug 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suggest you do not end-anchor the hostname, in case someone (or someone's browser or proxy) appends a port number, as in www.domain.foo:80. If this were to happen, an end-anchored pattern would fail.

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.[b]foo[/b]
RewriteRule ^d/(.*) http://d.domain.foo/$1 [R=301,L]

Jim

compooter

2:00 am on Aug 30, 2004 (gmt 0)

10+ Year Member



Ah thanks for that additional tip, jdmorgan.