add www only if is no subdomain present using htaccess - .htaccess

I need to automatically add www if there´s no subdomain in the url
https://example.com => https://www.example.com
http://test.example.com => http://test.example.com (no change)
it also will be nice to make it work for both http and https.
this is what I have so far
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[^.]+)$
RewriteRule ^ https://www.%2%{REQUEST_URI} [NE,L,R]

Related

Improve htaccess rewrite for SSL, non WWW using subdirectories

All the possible ways of writing a domain name should be rewritten to https and non-www. i.e.
example.com => https://example.com
www.example.com => https://example.com
http://example.com => https://example.com
http://www.example.com => https://example.com
https://www.example.com => https://example.com
Those rules also applies for all of the sub domains (https and non-www).
The main site should be rewritten to a folder called "main". At the moment I have one sub domain which should be rewritten to a folder called "sub".
So I got it working trough .htaccess but this code is a bit messy and I'm pretty sure there is a nicer/cleaner way to achieve what I want.
Could someone help me out to improve the following .htaccess?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/main/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /sub/$1
# Force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The URL of the main domain should be like: https://example.com and sub domain should be like https://sub.example.com
You can use these rules
RewriteEngine on
#redirect main site to https non www
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mainsite\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
#rewrite mainsite to /mainsite folder
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$
RewriteCond %{REQUEST_URI} !^/mainsitefolder
RewriteRule ^ /mainsitefolder%{REQUEST_URI} [L]
#redirect subdomain to https and subfolder
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.domain\.com)
RewriteRule ^ https://sub.domain.com%{REQUEST_URI} [NE,L,R]
#rewrite subdomain to subfolder
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subdomainfolder
RewriteRule ^ /subdomainfolder%{REQUEST_URI} [L]

Redirect NON-www to www for https

I've the following redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How can I redirect non WWW https to WWW?
The htaccess is located in the public_html folder
You can use the following :
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will redirect
http://example.com
https://example.com
to
https://www.example.com
I'm using this on a Joomla site and it redirects everything to https://www
I can't see this solution anywhere else so thought I'd share it
RewriteCond %{HTTP_HOST} ^domainname\.co.uk [NC]
RewriteRule ^(.*)$ https://www.domainname.co.uk/$1 [R=301,L]

redirect https to http for domain.com page only by using htaccess

I have purchased SSL certificate. And i have added https to all my web page using below script in htaccess file.
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
Here i need to redirect from https to http for my homepage only.
Example:
https://www.domain.com to http://www.domian.com
Other pages should be redirect like below.
http://domain.com/login to https://www.domain.com/login
http://www.domian.com/register to https://www.domian.com/register
Note:
I need to append www to all my URLs for SEO purpose.
Thanks in advance.
You can use .+ to not to match landing page in your regex:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.+)$ https://www.domain.com/$1 [L,R=301,NE]
However since you're also adding www I suggest following rules:
RewriteEngine On
# all pages except home page
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?!frontpageajax).+$ https://www.%1%{REQUEST_URI} [L,NC,R=301,NE]
# landing page redirect https=>http
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(frontpageajax/?)?$ http://www.%1 [L,NC,R=301]

Redirect HTTP to HTTPS excluding one sup domain and add www if needed

I want to redirect my SSL site (domain.com) with HTTPS protocol but not domain.org which is actually a sub-directory of domain.com (domain.com/domainorg/). I want this:
http://domain.com TO https://www.domain.com
http://domain.org TO http://www.domain.org
I tested my htacess on http://htaccess.mwl.be/ and it works fine but when I actually run it, I get “The page isn't redirecting properly” and it appears to be in a loop when I test all options.
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www.)?domain.org$
RewriteRule ^/?(.*)$ http://www.domain.org/$1 [L,R=301]
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
I am not proficient at htacess at all. What do I need to correct or change?
Why are some of your rules outside <IfModule mod_rewrite.c>? I'd put all of them inside it.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirecting domain.com http URLs to https (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.com non-www domain.com URLs to www (any non-https will be rewritten to https as well)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.org non-www URLs to www (any https will be rewritten to http as well)
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
# Redirecting domain.org https URLs to http (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
</IfModule>
You may also force URLs like this domain.com/domainorg/* to be redirected to http://www.domain.org/* using:
RewriteCond RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond RewriteCond %{REQUEST_URI} ^\/?domainorg(\/.*)?$ [NC]
RewriteRule ^.*$ http://www.domain.org%1 [R=301,L]

Redirect all url to non-www HTTPS affecting all other domains in root

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]

Resources