force outgoing links as http vs https on secure domain? (htaccess?) - .htaccess

I have forced a ecommerce site to be always in https, due to some issues with having only specific urls triggered as secure. I have used the following in my .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.vshoen.com/$1 [R,L]
But now my problem is all external links without www in the url open in https as well for some reason. Most of the time its not a problem but there are a few specific links that need to simply be http://url.com instead of http://www.url.com and for in this case they are not loading as its trying to open them as https://url.com
I was hoping there would be a resolution to this, this is a wordpress site if that makes any difference.
Thanks in advance.

Try this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

Related

https redirect for every url doesn't work

I have an issue that came out after I have successfully transferred my website from a server to another (it's always the same url). Basically I have a forums created with xenforo that can be reached at https://forums.mywebsite.com.
Of course you can log in with your account and save username/password in the browser if needed. The problem is the following:
With https://forums.mywebsite.com you are still logged
With http://forums.mywebsite.com you have to login every time
With www.forums.mywebsite.com you have to login every time
I guess that there is an issue with the cookies, because the cookies for the https work while the others not. My idea was to redirect every single url to an https url; so:
With https://forums.mywebsite.com > no changes
With http://forums.mywebsite.com > https://forums.mywebsite.com
With www.forums.mywebsite.com > https://forums.mywebsite.com
I really need this and telling users to use only https is not a great solution because there always be someone that uses http or www. My idea was this:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.forums.mywebsite.com%{REQUEST_URI} [R=301,L]
Anyway when I type www.forums.mywebsite.com I still don't get a redirect to https://forums.mywebsite.com. Do I have to change any config file?
I am editing this .htaccess that can be found at the root of the system as you can see here (taken from WinSCP).
On my Xenforo board I require the same thing, redirect everything to https://example.com without www.
This is what I use:
#Redirect www and http
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Never had a problem with it like this.

.htaccess redirecting both www and non-www to https

I am using .htaccess. I am trying to redirect http://www.example.co.uk AND http://example.co.uk to the secure https. I have searched on this site and tried all the suggestions, but none work. I have approached my hosting company and they have tried with no joy.
One example I used is -
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.co.uk$
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R,L]
Another example code I use is -
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://zapstudio.co.uk%{REQUEST_URI} [L,NE,R=301]
The error comes up as follows on Firefox:
The page isn’t redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
There is no other coding in my .htaccess file than the code inserted. Please, can anyone shed any light on this.

.htaccess : http to https redirection does not work

I have a problem with my .htaccess and https redirection.
First of all, I have consulted these links to find a solution but none could help me.
List of links :
.htaccess redirect http to https
ModRewrite with HTTPS
https://www.ndchost.com/wiki/apache/redirect-http-to-https
htaccess redirect to https://www
https://serverfault.com/questions/116206/how-do-i-use-htaccess-to-always-redirect-from-http-to-https
When I load the page with http, the .htaccess redirect me to https but when I load the page with https, I have an infinite loop.
Here is my .htaccess code :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^(api|www|dev|stefano|sav)\.
RewriteCond %{REQUEST_URI} !^/(api|www|dev|stefano|sav)/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*)$ %1/$1 [L]
Is anybody can help me create a redirect condition (http to https) ?
You are leaving off the Rewrite Flags You need to tell it force the redirection with R flag and optional provide status code which is recommended 301, 302 etc.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
By the way, every example you linked to shows the answer using the R flag, not sure why you didn't use exact examples :)
Solution if behind devices like load balancer.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have been fighting with this same problem for hours. It seems that I needed the solution for systems behind a load balancer. Perhaps this is because I am using Cloudflare with my site.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
After adding the above lines to my .htaccess file, I deleted the cache on Cloudflare and the site loads as it should.

Advice on Configuring .HTaccess file to Redirect HTTP Subdomain to HTTPS Equivalent

I'm sure there is an easy answer to this question, but I've scoured through through available threads and its either not there or I'm too ignorant to recognize it starting in my face. This seems to be the answer, but I just can't configure correctly.
Question: How do I set up .htaccess so that all subdomains are forwarded to their https equivalents, while also allowing the main domain itself get ported to its https equivalent? In other words:
hxxp://subdomain1.domain.com --> hxxps://subdomain1.domain.com
hxxp://subdomain2.domain.com --> hxxps://subdomain2.domain.com
...and so on, but also...
hxxp://domain.com --> hxxps://domain.com
Current Configuration:
My .htaccess is set up to provide an unconditional forward as follows:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
To achieve what I want, I would think that there must be some way to define a wildcard and pass the result of that wildcard as the subdomain - whether, subdomain1, subdomain2 or a value of nothing - to the actual redirect.
Any help would be much appreciated.
I haven't tested this, but it should allow you to capture the subdomain as well as the request.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.*)\.yourdomain\.com$
RewriteRule ^(.*)$ https://%1.domain.com/$1 [R,L]
This rule however, will not work for requests that do not contain a subdirectory, so you actually need two rules.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
Goto your subdomain folder
Create .htaccess file in this folder
and paste this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Forcing SSL & www subdomain for any URI entered with .htaccess?

I have an SSL certificate for the 'www' subdomain of a web site we'll call example.com.
Therefore I can force SSL, but if the user doesn't enter 'www.example.com' into their browser, the SSL certificate appears invalid.
I wish to accomplish this with mod_rewrite rules in an .htaccess file at the root of the web site.
No matter what URI the user enters, (e.g. example.com, www.example.com, https://example.com, example.com/folder/file.html, etc.), I want to force the HTTPS and the www subdomain, keeping whatever URI they provided, of course.
I was playing around with the .htaccess file and some rules, as I've already done some research, but I don't have much experience with mod_rewrite or .htaccess. I think I found the SVN revision that almost worked for me, but not quite. Here it is:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^(.*)$ https://www.example.com/$1 [r=301,nc]
Try not to laugh too hard at my file; it is mostly derived from examples I found online.
Any pointers are appreciated! =)
Try this:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]

Resources