RewriteCond A or (!B and !C) - apache

I can use mod_rewrite in .htaccess file on Apache server. I'd like to have something like this:
RewriteEngine On
RewriteCond %{HTTPS} !on [NC,OR]
RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} !^sub\.example\.com
RewriteRule ^/?(.*)$ https://example.com/$1
while meaning "if http or request subdomain other than sub.(e. g. www.), redirect to https and without www.".
https://example.com --> do nothing
http://example.com --> https://example.com
https://www.example.com --> https://example.com
http://www.example.com --> https://example.com
https://sub.example.com --> do nothing
http://sub.example.com --> https://sub.example.com, this will definitely not work with code above
What's the simplest way to do this? Thanks in advance!

You need to break this into 2 different rules:
RewriteEngine On
# remove www and https for https://www.example.com or http://www.example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=302]
# simply add https for rest
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
Make sure to clear your browser cache or use a new browser for testing.
Change 302 to 301 after testing.

Related

force redirection from https://example.com to https://www.example.com

I know this is a question that is asked several time. I tried several rules but redirection of https://example.com to https://www.example.com is not working.
My current redirection rule in the Apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
The above rule works fine for http://example.com and http://www.example.com
I find it. This needs to be added in the ssl vhost file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
My current redirection rule in the apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
You already have a solution, but as you have found, the above redirect will obviously only apply to HTTP requests when in the "vhost of non ssl". In this case, the HTTPS server variable is always "off" - so the first RewriteCond directive is entirely redundant.
However, you don't need mod_rewrite at all when redirecting from HTTP to HTTPS in the HTTP-virtualhost. A simple mod_alias Redirect will do the job much "better":
Redirect 301 / https://www.example.com/

How to redirect www to non-www https?

I have installed https on my domain. And all this cases are available of my website:
http://example.com // will be redirected to https://example.com
https://example.com
http://www.example.com // will be redirected to https://www.example.com
https://www.example.com
See? everything will be using https protocol. All fine.
Now I need to redirect all www to non-www. So http://www.example.com and https://www.example.com should be redirected to https://example.com (in other word, all cases should be redirected to this).
Here is my current code in the /etc/apache2/sites-avalible/000-default.conf:
<VirtualHost *:80>
.
.
.
RewriteEngine on
RewriteCond %{SERVER_NAME} =lamtakam.com [OR]
RewriteCond %{SERVER_NAME} =www.lamtakam.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] -- this
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] -- and this are for www redirection
-- but doesn't work
</VirtualHost>
any idea?
also lamtakam is my exact domain I was talking about, if you need to check something.
Edit:
Currently I have this inside .htaccess file on the root of my project:
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# this doesn't work on some version of xampp
# RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
ErrorDocument 404 /error404.html
If you want rules in VirtualHost then it would be 2 set of rules. If you can keep rules in site root .htaccess then it would be a single rule. I am providing .htaccess rule here:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure you remove your existing rules shown in VirtualHost above and you test in a new browser to avoid old browser cache.

Can't Force HTTPS and Non-WWW via htaccess

I am trying to force any regular http request to redirect to https, and also force non www in the URLs, so that every request in the end will be https://example.com
http://example.com becomes https://example.com
http://www.example.com becomes https://example.com
https://www.example.com becomes https://example.com
http://example.com becomes https://example.com
I have the following code in my htaccess file.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
But I am getting an error that states it has too many redirects. It seems to work fine on this test site with the correct output:
htaaccess tester preview
Any ideas or a better approach to this?
The following code should help you:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
It will redirect everything to https://example.com

htaccess https to https subfolder

I'm creating a puzzle/challenge website that requires there to be two different sets of served files, depending on whether or not the user typed http: or https:
It doesn't seem like I can just redirect all HTTPS traffic using my web-host to a subfolder, so I've been trying to edit the htaccess file, but I keep ending up with infinite redirects.
What I'm trying to do:
http://www.example.com/secure -> http://www.example.com
https://www.example.com -> https://www.example.com/secure
When I setup redirects, after chrome times out, I get https://www.example.com/secure/secure/secure/secure/secure/secure/secure ...
The redirects I've setup are:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule .* https://www.example.com/secure [NC,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^secure/ http://www.example.com/ [NC,L,R=301]
Neither rule seems to work, but the first rule seems to loop forever. Can anyone explain what I'm doing wrong?
The RewriteCond %{HTTPS} on portion may not work for all web servers. Some of servers are using RewriteCond %{HTTP:X-Forwarded-Proto} https.
I think you should do :
RewriteEngine On
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteCond %{REQUEST_URI} !^/secure
RewriteRule ^(.*)$ https://www.example.com/secure [NC,L,R=301]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} ^/secure
RewriteRule ^(.*)$ http://www.example.com/ [NC,L,R=301]

.htaccess allow https non www, force www on http only

Having troubles with Paypal subscriptions have held the old https://example.com domain in the paypal subscription profile and cannot be changed without cancel and re-subscribing.
what I have in place at the moment is forcing everything to http://www.example.com so http(s)://example.com 301 -> http://www.example.com
This is what I currently have.
paypal is trying to submit some subscriptions to https://example.com/payments/ipn.php it was doing a 301 redirect on https to http://www.example.com but you loose the POST data on a 301 redirect...
With the code below, https://www.example.com works, but since i need https://example.com for Paypal to work, i'm not sure how to allow the none www on the HTTPS domain without it redirecting to the HTTP host (http://www.example.com)
I added the robots.txt redirection to disallow / on https, this works as I don't want duplicate site on google.
I hope this has all made sense.
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} !^(/payments)
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots\.txt$ robots_ssl.txt [L]
HERE IS THE FIXED .htaccess for anyone that's interested!
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots\.txt$ robots_ssl.txt [L]
RewriteRule %{HTTPS_HOST} ^domain\.com%{REQUEST_URI}
To filter HTTPS you could use following RewriteCond:
RewriteCond %{HTTPS} !=on # if it's not HTTPS
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
Or you could avoid a redirect, when it's a POST request, so the POST data wont be lost:
RewriteCond %{REQUEST_METHOD} !=POST # if it's not POST
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]