Rewrite Rule .htaccess - .htaccess

I redirect all basic http requests to use https and this works however when I try to use www.example.com it fails, it only works for the url example.com or http://example.com. This is my code:
# for main domain
# RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
# RewriteCond %{HTTPS} off [OR]
# RewriteCond %{HTTP_HOST} ^www\. [NC]
# RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]s

Related

redirect www URL to non www with https

How can I redirect all www requests to non-www with https?
For example all following 3 URL`s:
http://www.example.com/about-us
http://example.com/about-us
https://www.example.com/about-us
Should redirect to https://example.com/about-us
I have tried answers posted at
redirection issue in www to non www with ssl/https
and
.htaccess redirect www to non-www with SSL/HTTPS but they are not working. My current rule is
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
Please guide.
Try the following code "
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://example.com/$1 [L,NE,R=302]
So , the scenario above is to catch all http with or without www by this condition RewriteCond %{HTTPS} off and then catch all www with this condition RewriteCond %{HTTP_HOST} ^www\. and then force all into https:// .
Note: clear your browser cache and test it and if it's Ok change 302 to 301 to get permanent redirection.
Update :
go to these lines in your .htaccess :
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
make them without # like this :
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
Then go directly to the code in the last of page :
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]
change it to this :
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]
Test it and make sure you clear your browser cache .
If it is ok , change 302 to 301 as i mentioned above .

How to redirect domain.com or www.domain.com to https www.domain.com

I want to redirect my domain http to https and I have already do that with below mention code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https www.domain.com/$1 [L,R=301,NC]
When a user enter domain.com it's automatically redirect to https www.domain.com But when user enter only www.domain.com it's redirect to the http www.domain.com
Please help me to solve this issue.
You can use:
RewriteEngine on
# Test without www
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
# Test for http
RewriteCond %{HTTPS} off
# Redirect to https www
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]
At your code :
RewriteCond %{HTTP_HOST} ^domain.com [NC]
It is a condition for the following rule ; so the rule will be applied only for none www request.
Moreover , don't use R=301 redirection when you test new code unless you make sure that your rules is ok and you could use R=302 or just R.
regardless of your code Put the following code at your main directory .htaccess :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
If you want to force every request to be www you should do this :
RewriteEngine On
RewriteCond %{HTTPS} !=on
#this line above will match any request without `https`
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
#the two lines above will exclude none `www` request and force only `www` request unto `https` and the rule will stop here `L`
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
# the last two line will exclude any `www` request and force only none `www` into `https`

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