Forum Moderators: phranque
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST) !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com
RewriteRule (.*) http://www.%1.example.com/$1 [R=301,L]
It works as expected for this input:
test.example.com -> www.test.example.com good
blah.example.com -> www.blah.example.com good
However, it also does this, which I don't want:
www.example.com -> www.www.example.com bad
I have also tried a less specific rule for the first RewriteCond:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST) !^www\.
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com
RewriteRule (.*) http://www.%1.example.com/$1 [R=301,L]
I've got other code in the .htaccess but it exhibits the behavior even if I strip all that out and just use the code shown above. Thoughts? :/
RewriteEngine on
#
# Externally redirect to add "www" to main domain or subdomains
RewriteCond %{HTTP_HOST) !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([a-z]+\.)?example\.com
RewriteRule (.*) http://www.%1example.com/$1 [R=301,L]
#
# Externally redirect to remove trailing dot and/or port number
RewriteCond %{HTTP_HOST} ^(([a-z]+\.)+)example\.com(\.¦:[0-9]*)
RewriteRule (.*) http://%1example.com/$1 [R=301,L]
Also note that I changed both rules.
Replace the broken pipe "¦" character in the RewriteCond pattern of the second rule with a solid pipe before use; Posting on this forum modifies the pipe character.
Jim