homepage Welcome to WebmasterWorld Guest from 54.197.215.146
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Problem redirecting folder to subdomain
I'm sure it's something simple!
isorg




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

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




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

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




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

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




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

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




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

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

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