Redirecting all requests to https:// - .htaccess

Our website is running on a nginx server and our .htaccess has following code
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
When I type domain.com it redirects to https://www.domain.com but when we type www.domain.com it remains unsecured.

Try :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Now the pattern matches both www and non-www requests for the domain.com

Related

Redirection from https:// domain alias to subfolder on a secured https://

I now have a https:// secure server.
But now the redirection does n’t work anymore.
The redirection e.g. from agenda.harmonicahoek.nl => https://www.harmonicahoek.nl/service/agenda/
This what is now in the .htacces:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.harmonicahoek.nl/$1 [L,R=301]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} agenda.harmonicahoek.nl [NC]
RewriteRule ^(.*)$ https://www.harmonicahoek.nl/service/agenda/ [r=301,NC]
But this does not work.
How will it work?

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]

htaccess Redirect https for specific directory

I'm trying to achieve the following:
Redirect all WWW requests to equivalent (without WWW)
Redirect all HTTPS requests to HTTP
Redirect all requests to subdirectory /cart to force HTTPS
Here is what I have but it's giving me a loop.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(cart/.*)$ https://example.com/$1 [R,L]
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
RewriteEngine On
#www to non-www
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
#https to http
RewriteCond %{HTTPS} ^on$
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
#if the request is for /cart then enable https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/cart
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

htaccess to redirect whole website to https doesn't work

I am using the below script to redirect traffic to https. It work fine if I just type domain.com in the address bar but if I type www.domain.com it doesn't get redirected. How do I make it redirect both with and without www?
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Try this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
In case you wish to force HTTPS for a particular folder you can use:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} somefolder
RewriteRule ^(.*)$ https://www.yourdomain.com/somefolder/$1 [R,L]
Thanks

Problem with subdomains using .htaccess to redirect non-www URLs to www (301)

I redirected non-www request to www through .htaccess Rewrite Rule.
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) www.%{HTTP_HOST}/$1 [L,R=301]
But now I am having problems with subdomains. When I am accessing touch.111.com then the above rule redirects to touch.www.111.com (which is not accessible), and the website breaks on touch devices.
Please advise me on how to fix the above problem.
You must be specific if you want to redirect only domain.com to www.domain.com and retain sub-domains (such as touch.domain.com) :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]
This is a generic solution that can work for any domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
see http://www.htaccessredirected.com/force-redirecting-non-www-to-www-htaccess.htm
# For sites running on a port other than 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R]
# And for a site running on port 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources