Redirect non-www to www https exclude subdomains

Richard G

Verified User
Joined
Jul 6, 2008
Messages
12,563
Location
Maastricht
Redirect to https and non-www to www except subdomains

I'm looking all over the internet, but nothing works like I want to, or with speed testers I get messages that I have to reduce redirects.
Most of what I found did not work or only worked half.

This is what I want to do:
1.) Redirect all http to https including subdomains.

2.) Redirect all http://domain.com to https://www.domain.com (so non-www to www)

3.) I do NOT want -any- subdomdain https://*(except www).domain.com to be redirected to https://www.*.domain.com
So I don't want http or https:/sub.domain.com redirected to https://www.sub.domain.com but only to https://sub.domain.com
Same for other subdomains.

If 3.) is not possible, I would like at least 1 subdomain from http to https without www but just as https://sub.domain.com.

I would like to do this in a .htaccess file in the best way possible (less redirects possible).
Please do not point to apache documentation, I've read that, I'm not English and I only get more confused. :D
 
Last edited:
Hello,

You can modify templates for this and split one virtual host for www.domain.com and domain.com into two different virtual hosts.

Please note there is only one way to avoid redirects it is not to use redirects at all, so you need to modify HTML pages of your sites.
 
Hello.

Thank you. I know it's better done via virtualhosts these days but I would like to keep the redirect for now.
 
Richard,

I mean that you can use directadmin templates to install redirects for all domains at once with or without setting redirects for subdomains.

If you don't want to split virtual hosts into two with www and without www then simply add a mod_rewrite rule into virtual host templates, but it might effect subdomains.
 
Hello Alex.

I could search for such template example. But I thought that for Google ranking (so SEO) reasons, it was better not to have to versions, but only 1 version, a www or a non-www version of a site/domain?
 
I'm looking all over the internet, but nothing works like I want to, or with speed testers I get messages that I have to reduce redirects.
Most of what I found did not work or only worked half.

This is what I want to do:
1.) Redirect all http to https including subdomains.

2.) Redirect all http://domain.com to https://www.domain.com (so non-www to www)

3.) I do NOT want -any- subdomdain https://*(except www).domain.com to be redirected to https://www.*.domain.com
So I don't want http or https:/sub.domain.com redirected to https://www.sub.domain.com but only to https://sub.domain.com
Same for other subdomains.

If 3.) is not possible, I would like at least 1 subdomain from http to https without www but just as https://sub.domain.com.

I would like to do this in a .htaccess file in the best way possible (less redirects possible).
Please do not point to apache documentation, I've read that, I'm not English and I only get more confused. :D

Subdomain when important on its own, handling separate from maindomain, then we use a separate account and creating subdomain ( same as creating maindomains) in that so also separate paths and all.
Also a lot of www thing you can handle better then while a www infront of subd.. is not done, but with maindomain it could be handy, some cms need a subdomain always in DA'if https i don't understand this but for long time now something with ssl sessions or so.
oftopic ok sorry, we handle that with our own subdomain ( then no www. needed)


So www. is in ( weze) basic ofcourse a subdomain to,

Which tool is saying to many redirects, probably a wrong order in htaccess, or you have 2 or more redirects after eachother where they could be in 1 rule!

YES soms cms have own redirects in scripts as also has the ones in htaccess made by yourself.
 
Last edited:
You're correct that www is in fact a subdomain name too. But I also just like it more with www then without www.

Which tool is saying to many redirects, probably a wrong order in htaccess, or you have 2 or more redirects after eachother where they could be in 1 rule!
I forgot the link, it was an online tool which checked the page loading of your site.

I did not have al ot of redirects, just something like this:
Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.mydomain.nl/$1 [R=301,L]

and some line more, but I can't remember because I change so much. It's just 1 subdomain needed in my case so for now I created a new .htaccess in that subdomain directory and that fixes it too. Not beautiful but it works.

I'm just wondering what to do if users want to have a lot of subdomains and redirects to ssl as well as from non-www to www. Because then this line won't work. And I tried lots of them.
Are they then bound to use non-www versions of their domain? Or.... can't find the correct solution which will do this automatically.
I found several "exclude all subdomains which are not starting with www" lines (to call it like that) but they don't seem to work.
On the other hand... this could also be because Firefox still is using some cache while it was disabled, that works also frustrating when testing things, especially if ctrl-F5 does not do the trick and you have to clear the cache manually via options (it's set to 1 mb).
 
You're correct that www is in fact a subdomain name too. But I also just like it more with www then without www.


I forgot the link, it was an online tool which checked the page loading of your site.

I did not have al ot of redirects, just something like this:
Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.mydomain.nl/$1 [R=301,L]

and some line more, but I can't remember because I change so much. It's just 1 subdomain needed in my case so for now I created a new .htaccess in that subdomain directory and that fixes it too. Not beautiful but it works.

I'm just wondering what to do if users want to have a lot of subdomains and redirects to ssl as well as from non-www to www. Because then this line won't work. And I tried lots of them.
Are they then bound to use non-www versions of their domain? Or.... can't find the correct solution which will do this automatically.
I found several "exclude all subdomains which are not starting with www" lines (to call it like that) but they don't seem to work.
On the other hand... this could also be because Firefox still is using some cache while it was disabled, that works also frustrating when testing things, especially if ctrl-F5 does not do the trick and you have to clear the cache manually via options (it's set to 1 mb).

OJA FF cache delete button and cookie delete addons while probably also sessions ssl cache active, in Edge is this sometimes more worse.

Pm for asking my phone number you can call then.

htaccess is not bad to redirect www and https why? wen Apache and no NINGX
 
Last edited:
Could ask the same question, why Nginx and not Apache? They both have their good and less things. Nginx does not support .htaccess I read. And we use mod_ruid2, don't know if Nginx can handle that too. And I don't have any issues with anything, while I read a lot of questions regarding stuff with Nginx here on the forums. ;)

I'll write you a pm.
 
Richard,

OK, probably I was not very clear. So my idea is as the following.

By default Directadmin creates a virtual host both for www.domain.com and domain.com. And if you want to redirect users in this case you need to use mod_rewrite.

And if you have numerous domains it might be an issue to update all them with a redirect. So here you can split domain.com and www.domain.com, the version for www.domain.com will be as usual, but without domain.com in alias.

The virtual host for domain.com will be shortened to:

Code:
<VirtualHost 1.2.3.4:80>
        ServerName domain.com
        Redirect 301 / http://www.domain.com/
</VirtualHost>

Something like that. Here you can redirect even to https://. I hope my idea is clear now ;)


I could search for such template example. But I thought that for Google ranking (so SEO) reasons, it was better not to have to versions, but only 1 version, a www or a non-www version of a site/domain?
 
Hello Alex.

Ah I think I understand better what you mean now.:)
It's a shared hosting server, but I only have two domains. One points to the other so I'm in fact only using 1 domain name and 1 subdomain.
I'll have a look at those templates, seems indeed a bit easier to maintain, the more since in apache 2.4 the redirect stuff is better not used anymore if possible I've read.

Thank you!
 
Back
Top