I want to rewrite http://, http://www. https://www. and nonexistent subdomain variations to https://domain.com. A nonexistent subdomain variation might be http://abc.domain.com and https://abc.domain.com. To complicate matters further, I want to keep trailing URLs, and prevent redirecting existing subdomains. AND, some existing subdomains use http while others use https.
This is what I have so far:
#don't redirect existing subdomains example1 or example2
RewriteCond %{HTTP_HOST} ^((example1|example2)\.)?domain\.com$
RewriteRule .* - [L]
##redirect http, wildcard subdomains, and www. with appended trailing URLs
RewriteCond %{HTTP_HOST} . [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
Thanks in advance.
Here is how you can redirect nonexistent subdomains to https://domain.com:
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(valid1|valid2|valid3)\.domain\.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
Related
I have two domains, a .eu domain and a .nl domain.
I've already added a rule that rewrites www to non www and http to https.
Now I need all pages to go to the .nl domain by default.
I've tried this:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%.nl{REQUEST_URI} [L,NE,R=301]
But that is not working.
What am I doing wrong?
You have error in your htaccess try with below, it is tested here.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.(.+)$
RewriteRule ^ https://%1.nl%{REQUEST_URI} [L,NE,R=301]
I have a cpanel account with maindomain.com and many other domains inside it.I used below code to Redirect all url to non-www HTTPS and put the .htaccess in root folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
But this is affecting all other domains too in ftp account , how to make this work only for the domain i want..For example maindomain.com
You can do this in a single rule like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://maindomain.com%{REQUEST_URI} [L,R=301,NE]
I've been trying to configure this for my website, but not being able to.
I use to have a cond on my .htaccess to force www for the main domain and nothing for subdomains, but since I got a SSL, I'm having some problems.
It's a wildcard SSL.
What I need is force HTTPS:// WWW on the main domain, and HTTPS:// on subdomains.
I.E: http://www.domain.com -> https://subdomain.domain.com
Is there any rule for that?
Thanks!
EDITED
Now I'm using like Jon posted
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [NC]
RewriteRule ^.*$ https://www.domain.com.br%{REQUEST_URI} [R,L]
# ---------- #
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
The thing is, when on main domain if I type HTTP:// with or without WWW, ir forces HTTPS:// and WWW, that's ok...
But on subdomain, when I type HTTP it doesn't force HTTPS, it redirects to the main domain only... that does not happen if I put a .htaccess inside the dir of the subdomain.
With a .htaccess inside my subdomain dir, if I type HTTP, it forces HTTPS normally...
Any suggestions?
Try:
RewriteEngine On
# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]
I have setup a sitewide 301 redirect in my .htaccess as follows
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
But I wanted to exclude subdomains (support, blog) from being included in this redirect. So I add the following RewriteConditions
RewriteEngine On
RewriteCond %{HTTP_HOST} !=support.example.com
RewriteCond %{HTTP_HOST} !=blog.example.com
RewriteCond %{HTTPS} !=on
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This works totally fine. However I wonder if there is a better way to specify the exclusion for all subdomains (not WWW) in just 1 RewriteCond statement (instead of a separate RewriteCond for each subdomain)
I have a few more subdomains and plan to add some more. Would appreciate the help.
You can use this rule:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Here's what I have so far:
#Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,L]
However, this messes up all subdomains doing the following redirect:
sub.domain.com -> www.sub.domain.com
And also, its dependant on the domain written on the remove trailing slash bit.
So... two questions.
How do I rewrite the rule on the "remove trailing slash" bit to exclude writing the domain on it?
How do I make a rewritecond to exclude subdomains, without explicitly writing them down, on the "force www." bit?
Examples of desired results -
sub.domain.com/something/ -> sub.domain.com/something
domain.com/something/ -> www.domain.com/something
www.domain.com/ -> www.domain.com
sub.domain.com -> sub.domain.com
Thanks!
Change the www to check for the actual domain:
#Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Or, if you're hosting a bunch of domains, you can check for a name before the TLD:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteRule ^ http://www.%1.%2%{REQUEST_URI} [L,R=301]
As for the trailing slash, you have to be careful that the request isn't made for a directory. Because if it is, and you have DirectorySlash turned on (by default it is on), then you'll cause a redirect loop.
To exclude subdomains, we assume that the first rule redirected the browser to ensure that it started with "www", and since subdomains aren't being redirected to start with "www", we can just check for that:
#Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]