2010-07-22 10 views
16

Tengo un proyecto de cliente en el que necesito forzar HTTPS para una determinada carpeta y forzar HTTP para todas las demás. Puedo implementar con éxito HTTPS para la carpeta que deseo, pero luego todos los enlaces al resto del sitio terminan en HTTPS. Me gustaría tener una regla que fuerce a las solicitudes de que cualquier cosa "no" en la carpeta segura sea forzada a regresar a HTTP. Esto es lo que tengo hasta ahora:Forzar HTTPS en ciertas URL y forzar HTTP para todas las demás

RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 

RewriteCond %{HTTPS} !=on 
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 

'mi' es el nombre de la carpeta que tengo que forzar HTTPS para.

¿Alguna idea?

Actualización: también probé:

RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 

# Force HTTPS for /my 
RewriteCond %{HTTPS} !=on 
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Force HTTP for anything which isn't /my 
RewriteCond %{HTTPS} =on 
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Remove index.php from URLs 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 

Pero en vez de solicitudes de/mi ser forzado a través de HTTPS que ahora acaba de resolver a http://www.example.com/index.php/my

:?

Respuesta

16

Ah, por supuesto. El problema radica en el hecho de que el conjunto de reglas de reescritura se volverá a procesar después de que se haya transformado en index.php siguiendo la redirección inicial. Con lo que tiene actualmente, necesita condicionar adicionalmente las redirecciones para asegurarse de que no se apliquen después de la reescritura en /index.php/my.

algo como lo siguiente debe hacer:

RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 

# Force HTTPS for /my 
RewriteCond %{HTTPS} !=on 
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/my [NC] 
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Force HTTP for anything which isn't /my 
RewriteCond %{HTTPS} =on 
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/my [NC] 
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Remove index.php from URLs 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 
+0

¡Gracias, me ha ahorrado mucho tiempo! FWIW, en Wordpress, el htaccess básico de Wordpress IfModule ya elimina index.php de las URL. – Jason

0

Sólo invertir las condiciones:

RewriteCond %{HTTPS} =on 
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 
+0

Hi ya, gracias por la respuesta rápida. Intenté eso pero no estaba seguro de dónde ubicarlo en el flujo de mis reglas existentes, ¿algún indicio? Si lo coloco por encima de la regla 'Forzar HTTPS' existente, impide que funcione 'Forzar HTTPS' y también evita que se ejecute la regla final que reescribe index.php. :? –

+0

@Nathan Pitman: No, dado que tienen diferentes condiciones de coincidencia, no deberían interactuar. – Gumbo

+0

bien, intenté con lo que sugeriste (ver la actualización de mi publicación original) pero esto no funcionó. Puedo ver que 'debería', pero creo que quizás tenga las reglas en el orden equivocado o ciertas reglas están causando que la reescritura termine antes de que se puedan evaluar otras condiciones y reglas ...:/ –

1

Esto es algo que funciona a partir de un sitio web de cliente de edad y podría ser adaptable para sus propósitos:

#If https off and in the cart dir 
RewriteCond %{HTTPS} =off [NC] 
RewriteCond %{REQUEST_URI} ^/cart/(.*) [NC] 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/cart/%1 [R=301,L] 

#If https on and not in cart dir  
RewriteCond %{HTTPS} =on 
RewriteCond %{REQUEST_URI} !^/cart [NC] 
#Above line actually used to read RewriteCond %{REQUEST_URI} !^/cart|media|images|thumbs|css|js [NC] 
#to allow js/css/images to be served so there were no mixed ssl messages popping up to visitors 
RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC] 
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 

Sustitución de la compra con mi quizá

4

Haga la siguiente prueba, debería funcionar para usted:

RewriteEngine On 

RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} ^/my 
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} !^/my 
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 
Cuestiones relacionadas