Welcome to WebmasterWorld Guest from 54.167.0.111

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Problem redirecting folder to subdomain

I'm sure it's something simple!

     

isorg

12:45 am on Apr 1, 2008 (gmt 0)

10+ Year Member



Hi,

I have used jdMorgan's code from [webmasterworld.com...] to setup some subdomain redirects in my httpd.conf.

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain>/<URLpath>
rewriteCond $1 !^subs/
rewritecond %{HTTP_HOST} !^www\. [NC]
rewritecond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$
rewriteRule (.*) /subs/%1/$1 [L]

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/?
RewriteRule ^subs/?(.*)$ [subs.example.com...] [R=301,L]

The subs.example.com/url -> www.example.com/subs/url internal rewrite works great.

The problem is that I can still access www.example.com/subs/url directly. I.e. it does not redirect me back to subs.example.com/url in the browser.

Any ideas what I'm doing wrong?! :-)

jdMorgan

1:41 pm on Apr 1, 2008 (gmt 0)

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



The warranty on "my" code is null and void if you modify it:

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/[^/]+/
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

Jim

isorg

2:09 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



Sorry for messing with the code! :-) I was only trying to make it work...

Unfortunately, I still can't get it to work! I am using a .info domain name with hyphens. Will that affect it?!

jdMorgan

3:42 pm on Apr 1, 2008 (gmt 0)

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



Sorry, it does work -- on dozens of servers. But I missed this in your post:
subdomain redirects in my httpd.conf.

The code changes subtly because of this, in that httpd.conf is a per-server context while .htaccess is a per-directory context. In a per-directory context, the URL-path examined by RewriteRule is stripped of the path to the current directory (where .htaccess file containing the rule resides). In httpd.conf (or conf.d, etc.), the URL-path must be complete (from DocumentRoot onward). The code I posted was for .htaccess.

For httpd.conf:


# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/[^/]+/
RewriteRule [b]^/s[/b]ubs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

Jim

isorg

11:50 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



It finally works! Thanks a million Jim. That '/' is small but vital :-)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month