I have a site example.com and also a subdomain and I want the following redirect:
subdomain.example.com -> www.subdomain.example.com
example.com -> www.example.com
I use the following code
RewriteEngine On
# Force www. always and SSL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
# Force SSL if already www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
Now the problem is that subdomain.example.com doesn't redirect but subdomain.example.com/ does.
I also get a Server Error when going to example.com, although it does redirect to www. correctly.
The point with these redirects is so that I can in the same .htaccess file in the root folder hopefully redirect the subdomains to subdomain.com more easily, I have several domains on one server.
I think you can combine the directives together like this:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
=> Check if HTTPS is off, or if the domain name does not begin with www, and then redirect to https://www.whatever.
Related
I am trying to redirect
example.com to www.example.com
and
http://example.come to https://www.example.com
HOWEVER, if a user types in
admin.example.com, http://admin.example.com or https://admin.example.com
it should NOT redirect to www.admin.example.com
What .htaccess can I use for this?
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Regex pattern (?:www\.)?([^.]+\.[^.]+) will match example.com or www.example.com but not admin.example.com
I want to redirect via .htaccess directives all these:
http://domain.com AND www.domain.com AND http://subdomain.domain.com
TO :
https://subdomain.domain.com
Please note that "subdomain" is an unique subdomain, I do not want to redirect any subdomain but only one.
I'm not familiar with .htaccess directives so I tried all RewriteRule or Redirect snippet found here or elsewere, but it's not working. I must miss something.
EDIT: I added a .htaccess into sur directory of subdomain with this and it works now:
It's OK, I added this in htaccess into the subdirectory of subdomain :
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Try with:
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
"domain.com" is my root domain and I have several other domains linked to it as alias domains - let's call those "domain2.com". They work exactly the same as the root but are used for alternate spelling and .net, .de, etc extensions.
I also have some domains that I bought for friends redirecting to their own webspaces - let's call those "domain3.com".
At last I have a single domain "domain4.com" that is also an alias but should behave differently.
I have listed my requirements here:
domain.com
-> root domain
-> wildcard SSL certificate
-> remove www
-> redirect anything to https://
-> "foo" subdomain is automatically redirected to /.sub-foo/ folder
(server setting that I have no influence in)
-> if possible preserve https://foo.domain.com/ in the address bar instead
of https://domain.com/.sub-foo/
domain2.com
-> alias domain linked to domain.com
-> no SSL certificate
-> redirect everything to domain.com, including Request URI and any subdomains
-> address bar should change to domain.com
domain3.com
-> alias domain linked to domain.com
-> redirect everything to external.domain.com, including Request URI
NOT including any subdomains
-> address bar should change to external.domain.com
domain4.com:
-> alias domain linked to domain.com
-> no SSL certificate
-> remove www
-> remove ANY subdomain used
-> remove ANY request URI used
-> ALWAYS redirect to the domain, no matter what
-> redirect that domain to the folder domain.com/subdir/
EDIT: Okay, after a whole lot of try and error I finally have found a mostly complete solution.
### domain3.com ###
RewriteCond %{HTTP_HOST} ^(.*\.)?domain3\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(\.sub-.*\/)?(.*)?$ [NC]
RewriteRule ^(.*)$ http://external.domain.com/%3 [L,R]
### domain4.com ###
# check if any subdomain is used and remove it
RewriteCond %{HTTP_HOST} !^domain4\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)?domain4\.com$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]
# check if redirected subdomain is used and remove it
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(\.sub-.*\/)(.*)?$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]
# check if any request is used and remove it
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(.*)$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]
# redirect to subdir/ directory
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteRule !^subdir/ subdir/ [L,NC]
### domain.com ###
# remove www subdomain and redirect to HTTPS
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
# redirect subdomain to https
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*\.)domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
# redirect http to https
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
### domain2.com ###
# remove www subdomain and redirect to HTTPS root domain
RewriteCond %{HTTP_HOST} !^domain2\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
# redirect subdomain to https root domain
RewriteCond %{HTTP_HOST} !^domain2\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)domain2\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
### copy domain2.com code for every other "domain2.com" like domain
### I have this code 6 times in my htaccess
For those who wonder about the 2nd RewriteRule part of the domain2.com - I can't use "https://%1domain.com/$1" which would be the "correct" approach, because my server alters the subdomain to "/.sub-foo/" BEFORE the .htaccess is processed.
The only thing NOT working (and I'm not sure I even will get this to work anyways) is that the address bar displays https://domain.com/.sub-foo/bar.php instead of https://foo.domain.com/bar.php
I'll try and talk to my hoster if he can deactivate the automatic redirect so that I can solve this via .htaccess alone.
If anyone has an idea how to shorten this monster - help would be appreciated.
I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following:
Redirect traffic to either http://mydomain.com or http://www.mydomain.com to use HTTPS
Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS
My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains.
Edit:
I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Figured it out. By moving these rules from the sites-available/default file to an .htaccess inside of the website root, I was able to get this working properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
basically what i want is redirect al request to use HTTPS instead of http
I have this in my htaccess so far and it worked great:
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</ifModule>
today someone noticed that when going to:
http://www.example.com it redirects to and shows an unsecure connection thingie.
My ssl is setup for non www domain: mydomain.com
So i need to make sure all site requests are sent to non www and https:
It works fine if i put example.com it redirects to https://example.com
but with www.example.com it goes to htts://www.example.com and shows the error
what do i need to add to my code to redirect www to non www and then to ssl
?
You will have to re-issue your certificate for both www and without www.
If someone connects to your site via a domain name that is not included in your common name, they will receive a warning.
The ssl negociation process happens before any response from the server (in your case, a redirection), so in all cases, your visitors will receive a warning when using a domain that is not in your common name.
You can get what you need from the HTTP_HOST
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
This way it will get the host always without the subdomain.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domain.com%{REQUEST_URI} [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule (.*) https://domain.com%{REQUEST_URI} [L,R=301,NC]
If you are using CloudFlare's free account then that's the problem. CloudFlare's free account does NOT support SSL Certificates. To continue using CloudFlare's free account with an SSL Certificate just go to the DNS settings in CloudFlare and take the orange cloud off of your domain and off of the cname WWW. That will fix your problem and cause both www and non-www to be redirected to https.
Also be sure to add this code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Then, everything should work!
This will redirect all of your www websites to non-www and secure them if you have completed the CERTBOT for each domain conf file. Put this in /etc/apache2/apache2.conf inside the Directory /www section:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
There is no need to CERTBOT a www domain after this code is inserted. Just do the domain.com choice. You do not need htaccess files. They can be restricted by the AllowOverride None selection.
Remember to restart apache.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]
Check out this:
RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]