V
V
Vladislav2017-01-16 14:53:44
Apache HTTP Server
Vladislav, 2017-01-16 14:53:44

How to properly set multiple overlapping RewriteRule conditions in .htaccess?

There is a website site.ru. Last fall, I received a certificate for it from the now unloved browser manufacturers startssl.com. I installed the certificate, checked that there was no mixed content left when manually typing with https, and left it like that until winter. Now, in the light of the upcoming version of Chrome, I decided that it would be time to set https as the main one, but I came across an unpleasant problem: all Android smartphones that fell into the hands of my certificate did not trust (Nougat did not check). I'm trying to make a redirect using .htaccess according to the following three conditions:
1) If the site is specified with a subdomain, redirect to the site without a domain. That is www.site.ru -> site.ru
2) If by %{HTTP_USER_AGENT} I think that the visitor is from the phone - leave the version of the site that he typed. Well, or you can redirect to http.
3) If by %{HTTP_USER_AGENT} I believe that the visitor is not from the phone - forcibly redirect to https.
In general, I can not combine the first condition with the other two. Either the www. is not being trimmed or I am getting the Too many redirects error. One of the latest attempts:

#Русские буквы я не использовал на сервере.
#Первое условие. Работает само по себе, не работает со следующим:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^site\.ru [NC]
RewriteRule (.*) https://site.ru/$1 [R=301,L]
#Другие попытки:
#RewriteRule ^(https?)\:\/\/([a-z0-9\-\.]*)(.*)$ https://site.ru/$3 [R=301,L]
#RewriteRule (.*) https://site.ru%{REQUEST_URI}

#Второе и третье условия:
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_USER_AGENT} !(?i:samsung|iphone|android)
RewriteRule (.*) https://site.ru/$1 [R=301,L]
#Другая попытка:
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

UPD:
I found such an option, tomorrow I will check how it will work together with the UserAgent check
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://site.ru/$1 [L,R=301]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2017-01-17
@Sabin

It seems that for RewriteRule it is impossible to allow situations where 2 conditions are suitable at once. At the moment I wrote:

#Desktop HTTP and WWW
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^site\.ru [NC]
RewriteCond %{HTTP_USER_AGENT} !(?i:samsung|iphone|android) [NC]
RewriteRule (.*) https://site.ru/$1 [L,R=301]

#Desktop HTTP
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^site\.ru [NC]
RewriteCond %{HTTP_USER_AGENT} !(?i:samsung|iphone|android) [NC]
RewriteRule (.*) https://site.ru/$1 [L,R=301]

#Desktop HTTPS and WWW
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^site\.ru [NC]
RewriteCond %{HTTP_USER_AGENT} !(?i:samsung|iphone|android) [NC]
RewriteRule (.*) https://site.ru/$1 [L,R=301]

#mobile WWW
RewriteCond %{HTTP_HOST} !^site\.ru [NC]
RewriteCond %{HTTP_USER_AGENT} (?i:samsung|iphone|android) [NC]
RewriteRule (.*) http://site.ru/$1 [L,R=301]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question