Forum Moderators: phranque
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?! :-)
# 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]
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]