Spirogg

Well-Known Member
Feb 21, 2018
700
161
43
chicago
cPanel Access Level
Root Administrator
yes

In redirects - add domain and set redirect only with www to Website Domain Names, Online Stores & Hosting - Domain.com

doesn't work
@vincentg

in .htaccess use this and try

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} www.domain.com
RewriteRule (.*) https://domain.com/$1 [R=301,L]
or you can try this as well works if you want https ON
Code:
# 301 REDIRECT HTTP TO HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# 301 REDIRECT WWW TO NON-WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
[\code]
 
Last edited:

Spirogg

Well-Known Member
Feb 21, 2018
700
161
43
chicago
cPanel Access Level
Root Administrator
1) from www to Non-www redirection
I have added here seven examples script for www to Non-www redirection.

# Example - 1 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
# Example - 2 #
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Example - 3 #
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]
# Example - 4 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) https://domain.com/$1 [R=301,L]
# Example - 5 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
# Example - 6 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule (.*) https://domain.com/$1 [R=301,L]
# Example - 7 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
2) from Non-www to www redirection
I have added here five examples script for www to Non-www redirection.

# Example - 1 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
# Example - 2 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Example - 3 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
# Example - 4 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
# Example - 5 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
3) https redirection
For https redirection form www to Non-www and Non-www to www add, s to any of the above examples to trick it.


4) For sub-folder redirection
For sub folder redirection. Like if you have redeveloped your website and the contents are stored at “/demo’ folder and if you want that to be redirected to the main domain in sub folder, then try any of the below examples.

# Example - 1 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/demo/$1 [R=301,L]
# Example - 2 #
Code:
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] 
RewriteRule ^$ http://%{HTTP_HOST}/demo/index.php [R,L]
# Example - 3 #
Code:
RewriteEngine On
RewriteRule ^$ /demo [L]
# Example - 4 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
RewriteRule ^$ demo [L]
or
RewriteRule ^$ /demo [L,R=301]
# Example - 5 #
Code:
RewriteEngine On
RewriteRule ^(.*)$ /demo/$1 [L]
# Example - 6 #
Code:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) https://www.domain.com/demo [R=301,L]
# Example - 7 #
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ demo [L]
# Example - 8 #
Code:
RewriteEngine On
RewriteRule !^demo/ /demo%{REQUEST_URI} [L,R=301]
# Example - 9 #
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /demo/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ demo/index.php [L]
# Example - 10 #
Code:
Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /demo/$1 [L]
# Example - 11 #
Code:
Options -Indexes +SymLinksIfOwnerMatch 
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
RewriteRule ^(.*)$ /demo/$1 [L]
options to try

also for anyone else needed to try .htaccess for redirects.
Spiro
 

Spirogg

Well-Known Member
Feb 21, 2018
700
161
43
chicago
cPanel Access Level
Root Administrator
this one seems to work really well on my site.
no matter what I type, it will always redirect to non-www https:// domain.com
Code:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [L,NE,R=301]
 

vincentg

Well-Known Member
May 12, 2004
177
5
168
new york
RewriteEngine on

AuthName httpsend.com

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.httpsend\.com$
RewriteRule ^/?$ "https\:\/\/httpsend\.com\/" [R=301,L]

That's all that's there

Cpanel created that redirect
I tried other ways of doing it

if I remove
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on

it works for www but it still redirect to https:

This is not working proper.

It should not matter what way you do a redirect.
If you do it correct it should work.

So the setting in domain is off
there is no RewriteCond %{HTTPS} off condition
nothing telling it to redirect to https://

The redirect that cpanel makes should work.
That's what it's there for - so techs like us don't have to manually do it.
 

vincentg

Well-Known Member
May 12, 2004
177
5
168
new york
If I only have a www redirect it works
Add others and it stops working.
It erratic behavior

Am tired of trying to make a broken system unbroken
 

vincentg

Well-Known Member
May 12, 2004
177
5
168
new york
We have more info on it.
It seems that if you have an account that had no redirects ever created it works normal
But if you create any type of redirect it will auto add redirection to SSL

Remove the redirect and the auto redirect to ssl remains
I believe this is a mistake from cpanel

This server is a new install
cPanel & WHM v102.0.15 (STANDARD)
AlmaLinux v8.6.0
 

Helena Rox

Registered
May 23, 2022
2
0
1
Italy
cPanel Access Level
Website Owner
Have a second site on server with same problem
Auto redirecting to SSL and can't redirect www
In checking I find some work and some don't.

From where I stand the problem is without a doubt due to this new setting in Domains