Keep HTTP or HTTPS on .htaccess - apache

I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} /api/profile/
RewriteRule ^(.*)$ http://whatsee-profile.s3.amazonaws.com/$1 [R,L]
It redirects my fake links to the real ones and works great! But I need to keep the original HTTP or HTTPS protocol before the url. I've tried the following, but it doesn't work.
RewriteEngine On
RewriteCond %{REQUEST_URI} /api/profile/
RewriteRule (http|https):\/\/whatsee.plugapps.net\/api\/profile\/(.*) $1://whatsee-profile.s3.amazonaws.com/$2 [R,L]
Any idea what is wrong? Thanks for your help!

It exists several ways of doing it.
Here is one working solution
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ - [env=protoc:https]
RewriteCond %{HTTPS} off
RewriteRule ^ - [env=protoc:http]
RewriteCond %{REQUEST_URI} /api/profile/
RewriteRule ^(.*)$ %{ENV:protoc}://whatsee-profile.s3.amazonaws.com/$1 [R,L]

Related

.htaccess - Redirect domain/lang/ to subdomain for each language

I've been struggling with this for weeks and I can't get to the solution, I have to make a redirect in .htaccess of a multi-language website with all its pages following the same structure but with a subdomain for each language. In the following way that is valid for http as https:
domain.es/es/allpages to es.subdomain.com/allpages
domain.es/pt/allpages to pt.subdomain.com/allpages
domain.es/de/allpages to de.subdomain.com/allpages
domain.es/fr/allpages to fr.subdomain.com/allpages
domain.es/en/allpages to eu.subdomain.com/allpages
Any ideas?
I have tried the following but not sure if it is correct:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) https://es.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/pt/(.*)$
RewriteRule ^(.*) https://pt.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/fr/(.*)$
RewriteRule ^(.*) https://fr.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/de/(.*)$
RewriteRule ^(.*) https://de.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/en/(.*)$
RewriteRule ^(.*) https://eu.subdomain.com/%1 [R=301,NC]
Thanks a lot!!!!
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.es$ [NC]
RewriteRule ^(es|pt|de|fr|en)/(.*)/?$ http://$1.subdomain.com/$2 [NE,R=301,NC,L]

cpanel .htaccess is not redirecting to HTTPS

I'm missing something simple I'm sure, some insight would be very helpful. I am working on setting up web site on a vps using cpanel. I'm trying to get it to always redirect to https instead having both available, I can do this without cpanel, but seem to be stumped when cpanel gets involved. I saw this, but it was zero help
https://www.namecheap.com/support/knowledgebase/article.aspx/9770/38/how-to-force-https-using-htaccess-file-in-cpanel
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Try writing your RewriteRule like this:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L,NE]
Make sure you clear your cache before you test this. I've set the flag to R, but if you're happy with the redirection then change it to R=301 as that will make it a permanent redirect.
You can always use a different .htaccess rule to redirect to HTTPS. I would re-write your .htaccess to the following (hardcoding your domain):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>

.htacess redirect for ip to https doesnt work well

I have tried to move my wordpress website to https.
I have dedicated ip also first i had this code below. But did'nt resolve my problem exactly
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.11$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Later on I had this code which resolved url redirection problems But later on ı had the same problems about when somebody cliks a http link given before doesnt go to https url.
<IfModule mod_rewrite.c>
RewriteEngine on
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
And another question is how i will redirect ip to https? And where i must put that?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
should indeed work
I used a plugin called Really simple SSl
And that entered this code.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
and before all (as mentioned by Michael Berkowski )
RewriteCond %{HTTP_HOST} 11\.11\.11\.11
RewriteRule ^(.*) https://www.example.com/$1 [L]
thanks to participants

Forcing https on the whole site but not on one folder

Could anyone tell me how I can force https on my whole website but not on a single folder or url.
At the moment I have this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]
But if I add this second code in the root htaccess to remove https from the /thatsite.com/printing folder, I get a redirect loop because I am forcing on the code http to https and not https to http...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(printing)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(printing)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Do you know a way around this please? I have been looking all over the internet and cannot find a single good answer.
Try these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/printing [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /printing [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I solved this issue by placing this code in an .htaccess file the root folder of the directory that hosts the http site (e.g. public_html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]

How to force ssl with htaccess?

How to force SSL with htaccess
With this code i get a redirect error in chrome:
RewriteEngine On
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule . https://%{HTTP_HOST}/%{REQUEST_URI} [L] HTTPS_HOST ???
Thanks
You can use that:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
I suggest you use this code and its working fine
#Force SSL
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}