htaccess add www if not subdomain, if subdomain remove www
Asked Answered
K

1

8

I'm trying to make this - probably example would be the best, so:

domain.com -> www.domain.com
www.domain.com -> www.domain.com
subdomain.domain.com -> subdomain.domain.com
www.subdomain.domain.com -> subdomain.domain.com

So if visitor is in subdomain, don't add www and when is set, remove It. Else if not in subdomain, add www. I tried to write something like this, but this doesn't remove www from subdomain adress :/ Also, if possible, I don't want to use domain and TLD domains but make it as much universal as possible. Could you help me where might be the problem please?

# add www if not subdomain
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]


# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.%{HTTP_HOST}+$ [NC]
RewriteRule (.*) http://%1.%{HTTP_HOST}/$1 [R=301,L]

This file I have in root:

# htaccess rules for subdomains and aliases
# to create new subdomain, create a folder www/subdom/(subdomain name)
# to create web for alias, create a folder www/domains/(whole domain name)

# htaccess pravidla pro subdomeny a samostatne weby aliasu
# pro vytvoreni subdomeny vytvorte adresar www/subdom/(nazev subdomeny)
# pro vytvoreni webu pro alias vytvorte adresar www/domains/(cely domenovy nazev)
# dalsi info a priklady: http://kb.wedos.com/r/32/webhosting-htaccess.html

RewriteEngine On

# cele domeny (aliasy)
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]

# subdomeny (s nebo bez www na zacatku)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

# aliasy - spravne presmerovani pri chybejicim /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

# subdomeny - spravne presmerovani pri chybejicim /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]
Kanter answered 9/2, 2016 at 6:10 Comment(0)
L
7

Have it like this:

# add www if not subdomain
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Lasser answered 9/2, 2016 at 6:13 Comment(3)
Great. Anyway there is problem it rewrite me this: www.subdom.site.com to this: subdom.site.com/domains/subdom.site.com I add file which is in root and which could give you better view on my problemArgentous
Your .htaccess doesn't show these 2 rules. Keep these 2 rules just below RewriteEngine On line and clear you browser cache before testing it.Lasser
OMG! I thought It never works, as I tried tons of solutions here and everywhere, but your code is working like a charm. Thank you Thank you :)Boldface

© 2022 - 2024 — McMap. All rights reserved.