Forum Moderators: phranque
[domain.com...]
TO
[test.domain.com...]
An I would also love it to work fine if the user type:
[test.domain.com...] inside the browser.
When i type: www.test.domain.com in the browser, it goes to domain.com, instead of to the sub directories: /music/artiste
This is what I have:
rewriteCond $1 !^music/
rewriteCond %{HTTP_HOST} !^www\.domain+\.com
RewriteCond%{HTTP_HOST} !^www\.domain+\.com
RewriteCond%{HTTP_HOST} ^([^.]+)\.([^.]+)\.com
rewriteRule (.+) /music/%2/$1 [L]
Thank you
I will proceed on two assumptions: First, that your example subdomain "test" was meant to be the name of an artist, and second, that while you were initially testing without the leading "www.", the back-reference in the RewriteRule substitution was %1, not %2. I cannot explain or understand your test results otherwise.
This code rewrites client requests for URLs of the form www.<name>.example.com/<anything> and <name>.example.com/<anything> to the filepath /music/<name>/<anything>, while excluding requests for www.example.com URLs from being rewritten.
RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %2 !=www
RewriteRule ^(.*)$ /music/%2/$1 [L]
Having done that, you can then rewrite only <artist>.example.com to your /music subdirectories and so avoid problems with duplicate content in search :
# Externally redirect requests for www.<artist>.example.com to <artist>.example.com
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
#
# Internally rewrite requests for <artist>.example.com/<anything>
# to /music/<artist>/<anything>, excluding "www"
RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /music/%1/$1 [L]
If the any of the above isn't clear, please refer to the resources cited in our Forum Charter.
[edit] Correction to third RewriteCond in first rule as noted below. [/edit]
Jim
[edited by: jdMorgan at 12:49 am (utc) on Dec. 6, 2009]
This one does the trick, But if i type: http://www.example.com/ it rewrites to: music/www , instead of rewriting to the: main index.html on http://example.com/index.html
I appreciate your help Jim an everyone else.. thanks
[edited by: jdMorgan at 12:47 am (utc) on Dec. 6, 2009]
[edit reason] example.com [/edit]
# Fix missing trailing slash, regardless of www preceding <artist> subdomain
# Externally redirect requests for www.<artist>.example.com/<something> or
# <artist>.example.com/<something> to <artist>.example.com/<something>/
# (Fix both incorrect www.<artist> hostname and add missing trailing slash)
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %2 !=www
RewriteRule ^(.*[^/])$ http://%2.example.com/$1/ [R=301,L]
#
# Externally redirect requests for www.<artist>.example.com/<anything>
# to <artist>.example.com/<anything> (Fix hostname only)
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
#
# Internally rewrite requests for <artist>.example.com/<anything>
# to /music/<artist>/<anything>, excluding "www"
RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /music/%1/$1 [L]
RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %2 !=www
RewriteRule ^(.*[^/])$ [%2.example.com...] [R=301,L]
bear with me thanks
Remember to completely flush (delete) your browser cache before testing any new code on your server.
Jim