Merging a HTTPS redirect with WWW redirect - apache

I currently have this in my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This ensures all http://www.example.com is redirected to http://example.com
I would like to implement an SSL certificate and I found this is the rewrite rule I must use for http to redirect to https.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
How could I merge the HTTPS condition with my current condition so that
http://www.example.com redirects to https://example.com
http://example.com redirects to https://example.com
If it is not possible to merge this, can I just have those two code snippets one after another?
Thanks in advance.

Yes, you can use them one after another. I'd also save your users a redirect by having your first set of rules send them straight to HTTPS.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

Using multiple rewrite rules .htaccess

I made the switch of my website from http to https. Now, I want to 301 redirect all http content to https.
I want to use this rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The issue is I also have another rule already in place which redirects all non-www pages to www ones.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]
How can I combine it so whatever link users write (http non-www ; http www ; https non-www) all redirect to https://www.example.com
Cheers!
You can use this single rule to add www and http->https in single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
This rule should be kept just below RewriteEngine line and make sure to clear your browser cache when testing this change.

Redirect subdomain to folder infinitely redirects due to www rule

I'm trying to add a single subdomain to an existing website. We've added the appropriate 'A' record. We want staging.example.com to display the contents from example.com/test/ (and remain staging.example.com).
The .htaccess file (written by someone else) redirects all non-https to https, and all non-www to www:
RewriteEngine On
RewriteBase /
#Rewrite http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
#Rewrite non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
I tried to add a line like the following, just before the last RewriteRule:
RewriteCond %{HTTP_HOST} !^staging\.(.+)$ [NC]
However, it always gets in a redirect loop to www.staging.example.com ...
Suggestions much appreciated.
This is because you are directing BACK to HTTP with your WWW redirect. Change it to https:
EDIT
I would also invert your statments .. This is EXACTLY what I use -- And I can verify it works for me:
RewriteEngine On
# to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Apache2 redirect to another domain with ssl

I have a no clue why this fails. I just want to redirect all domain to www.maindomain.com and also http to https, what am i missing?
# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# redirect without www to www
RewriteCond %{http_host} ^maindomain.com [nc]
RewriteRule ^(.*)$ https://www.maindomain.com [r=301,nc]
# redirect another domain to www.maindomain.com
RewriteCond %{HTTPS} off # this i was missing
RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain.com [NC]
RewriteRule ^(.*)$ https://www.maindomain.com [R=301,L]
http://maindomain.com to https:/www.maindomain.com/ works
http://anotherdomain.com to https:/www.maindomain.com/ works
https://anotherdomain.com to https:/www.maindomain.com/ fails
The Http to Https redirection for Another domain failed because Your Rule is missing the following line :
RewriteCond %{HTTPS} off
Try :
# redirect another domain to www.maindomain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain.com [NC]
RewriteRule ^(.*)$ https://www.maindomain.com [R=301,L]
You can try this:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Just copy and paste the above code in .htaccess file then the whole website will be redirected to “https” when the browser is opened in “http” mode. The browser just get redirected using url rewriting in .htaccess.

redirecting from www to non-www urls with .htaccess

I am redirecting an application with following code on my .htaccess file, the page is supposed to do the followings:
replace .php extension with .html
redirect from http to https
redirect from www to non-www urls
The extension .html is working fine and it is redirecting from http to https but the issue is to redirect from www to non-www, it is working properly on main url but when there is reference to a file then it is not working.
Say when i write www.ntestechnologies.com i get my desire url that is https://ntestechnologies.com but when i write www.ntestechnologies.com/index.html i get this https://www.ntestechnologies.com/index.html i don't need the www in this url as well please guide me, here is the code on htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.ntestechnologies\.com$
RewriteRule ^/?$ "https\:\/\/ntestechnologies\.com\/$1" [R=301,L]
RewriteRule ^(.*)\.html$ $1.php [nc]
You need only one RewriteEngine On.
You cannot use HTTP_HOST or REQUEST_URI in a RewriteRule. If you need to capture these values, you must do so in a RewriteCond
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(.+)
RewriteRule .* https://%1/$0 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1/$0 [R,L]
This removes the leading www, if present. At the same time, it redirects to HTTPS.
RewriteEngine On
# Redirects from HTTP to HTTPS. We use %{SERVER_PORT} as it's more reliable than %{HTTPS}
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Redirects www.example.com/... to example.com/...
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1%{REQUEST_URI} [R,L]
RewriteRule ^(.*)\.html$ $1.php [nc]

RewriteCond and RewriteRule in .htaccess

I have a client folder located at http://www.example.com/client
However, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client
Anybody know how to do that?
I've redirected my domains in the past like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/client if for example, http://www.example.co.za/client is entered.
Try this:
RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteRule ^/?$ https://www.example.com/client [301,NC,L]
It tells the apache, whenever the url is https://www.example.com or either with slash at the end, will redirect to ur /client