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]
Generally, the www prefix is not used when using subdomains. If you want to use the prefix with your subdomain, you can make a vhost file and add this behavior as part of your domain configuration.
The subdomain is what goes before the SLD. The most common subdomain is www, which stands for World Wide Web. This subdomain contains a website's homepage and its most important pages.
remember, the www is just a subdomain. Also no www is a commonplace these days, so maybe make the www.foo.com redirect to foo.com. It's even easier to type "google <Ctrl-Enter>", which adds both "www." and ".com" in most(?) browsers.
As a general rule, subdomains do not use www. If you need your subdomain to work with www, make sure to create the corresponding www record for your subdomain in addition to that for your domain.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With