.htaccess: перенаправить http на https или https на http в зависимости от URL - PullRequest
0 голосов
/ 07 февраля 2012

Приведенное ниже решение охватывает необходимые правила:

1) http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home всегда http, даже если вместо http введен https;

2) все остальные страницы всегда https, даже если вместо https был введен http;

но на практике НЕ охватывает

3) //www.mydomain.com/administration/any_file_in_admin_folder.php также всегда должен быть https (даже с параметром? P = home и т. Д.).

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]


#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Как реализовать 3) чтобы этот код работал? (Мне все еще не повезло, и мне нужна помощь). Спасибо.

Ответы [ 3 ]

2 голосов
/ 07 февраля 2012

Попробуйте это: Добавьте первую строку во 2-й и 3-й блок.

т.е. RewriteCond %{REQUEST_URI} !/administration/ [NC]

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#Requested URi must not have administration in it
RewriteCond %{REQUEST_URI} !/administration/ [NC]
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]  [L]

#all pages that are supposed to be http redirected if https
RewriteCond %{REQUEST_URI} !/administration/ [NC]
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
1 голос
/ 07 февраля 2012

Вторая часть вашего правила, RewriteCond %{QUERY_STRING} ^$, соответствует любому URL без строки запроса.Ваш URL //www.mydomain.com/administration/any_file_in_admin_folder.php не имеет строки запроса.Таким образом, IS_HTTP устанавливается на 1, а ваш пользователь перенаправляется на HTTP.

Попробуйте это.Это не проверено - но в основном вы сначала идентифицируете «домашнюю» строку запроса, а затем обрабатываете http://www.mydomain.com отдельно.

#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteRule ^$ - [E=IS_HTTP:1]

Это также может помочь:

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{REQUEST_URI} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
1 голос
/ 07 февраля 2012

Измените ваше первое правило на:

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#or if request URI is: /administration/any_file_in_admin_folder\.php
RewriteCond %{REQUEST_URI} !^/*administration/any_file_in_admin_folder\.php$ [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
...