2009-09-25 14 views
33

Ho diversi domini che operano con un singolo file .htaccess, ognuno protetto tramite SSL. Devo forzare https su tutti i domini assicurando inoltre il reindirizzamento di www a non-www. Ecco quello che sto usando che non funziona:.htaccess reindirizza www a non-www con SSL/HTTPS

RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST} 
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L] 

Esempio:

https://www.strategice.com 

dovrebbe reindirizzare a ...

https://strategice.com 

Grazie in anticipo!

+2

Segna una risposta corretta già! –

risposta

16

La tua condizione non sarà mai vera, perché è come "if (a == a + b)".

mi piacerebbe provare il seguente:

RewriteCond %{HTTP_HOST} ^www\.(.*) 
RewriteRule ^.*$ https://%1/$1 [R=301,L] 

Ciò catturare "google.com" da "www.google.com" in% 1, il resto in $ 1 e dopo che combinando il 2, quando HTTP_HOST inizia con www (con o senza https).

+0

Sfortunatamente questo crea ancora un errore "Connessione non attendibile" quando qualcuno tenta di accedere a https://www.domain.com – aegenes

+0

Hmm ma sta reindirizzando correttamente? Se è così potrebbe essere un problema di certificato. – Fabian

+2

Vedere [questo] (http://stackoverflow.com/a/10726167/372643) per il problema del certificato. – Bruno

2

questo ha funzionato per me:

RewriteEngine On 
RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} ^www\.(.*) 
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] 
5

certificato deve coprire sia www e non www https. Alcuni certificati del provider coprono entrambi per www.xxxx.yyy, ma solo uno per xxxx.yyy.

Accendere riscritture:

RewriteEngine On 

fare tutti https uso http:

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://xxx.yyy/$1 [L,R=301] 

solo fare https www usano il non www https:

RewriteCond %{SERVER_PORT} 443 
RewriteCond %{HTTP_HOST} ^www[.].+$ 
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [L,R=301] 

Non può essere l'elaborazione non -www https, altrimenti si verifica un loop.

In [L, R = 301]:

  1. L = Se la regola è stata elaborata, non elabora più.
  2. R = 301 = indica al browser/robot di eseguire un reindirizzamento permanente.

più generico

Un approccio più generico - non porta-dipendente - è:

RewriteCond %{HTTP_HOST} ^www\. 
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [R=301,QSA] 

per fare qualsiasi URL con www cadere.

RewriteCond %{HTTPS} !on 
RewriteCond %{HTTPS} !1 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTP:X-Forwarded-SSL} !on 
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [R=301,QSA] 

forzare qualsiasi URL non HTTP, anche per quei sistemi valle carico equilibratrici che cadono https, utilizzare HTTPS.

Si noti che non ho testato le opzioni forwarded, quindi apprezzerei il feedback su eventuali problemi con loro.Queste linee potrebbero essere tralasciate se il tuo sistema non si trova dietro un bilanciatore del carico.

PER HTTP_HOST o no

È possibile utilizzare ${HTTP_HOST} di essere parte dell'URL nel RewriteRule, oppure è possibile utilizzare il tuo esplicito canonico nome di dominio di testo (xxxx.yyy sopra).

Specifica il nome di dominio in modo esplicito assicura che nessun mezzo di caratteri-bending lieve-di-mano sono utilizzati nell'URL fornito dall'utente possibilmente ingannare il vostro sito nel fare qualcosa che potrebbe non essere preparato per, o almeno assicura che il il nome di dominio corretto appare nella barra degli indirizzi, indipendentemente da quale stringa di URL ha aperto la pagina.

Potrebbe anche aiutare a convertire domini codificati con punycode per mostrare i caratteri Unicode corretti nella barra degli indirizzi.

+0

Funziona come un incantesimo! –

+0

Funziona incredibilmente! Grazie per la spiegazione. –

44

www per non www con https

RewriteEngine on 

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] 

RewriteCond %{HTTPS} !on 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
+1

"RewriteRule^(. *) $ Http: //% 1/$ 1 [R = 301, L]" è stato modificato in "RewriteRule^(. *) $ Https: //% 1/$ 1 [R = 301, L ] "(by @KenVerhaegen) altrimenti" dovrebbe prima reindirizzare a non-http, quindi a https ... così ora possiamo evitare il secondo reindirizzamento eventualmente attivato. " – alexbt

1
RewriteEngine On 

RewriteCond %{HTTP_HOST} ^www\.(.*) 
RewriteRule ^.*$ https://%1/$1 [R=301,L] 

RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 

questo ha funzionato per me dopo molti tentativi ed errori. La prima parte è da parte dell'utente di cui sopra e catturerà www.xxx.yyy e inviare a https://xxx.yyy

Parte 2 guarda URL inserito e controlla se HTTPS, se non, invia a HTTPS

Fatto in questo ordine, segue la logica e non si verifica alcun errore.

Ecco la mia versione FULL in .htaccess fianco con WordPress:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www\.(.*) 
RewriteRule ^.*$ https://%1/$1 [R=301,L] 

RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 


# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

# END WordPress 
2

Per far rispettare non www e https in una singola richiesta, è possibile utilizzare la seguente regola nel vostro .htaccess:

RewriteEngine on 

RewriteCond %{HTTP_HOST} ^www\. [OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ 
RewriteRule^https://%1%{REQUEST_URI} [NE,L,R] 

Questo reindirizza http://www.example.com/ o https://www.example.com/ a ssl non www https://example.com/.

Sto usando R Indicatore di reindirizzamento temporaneo a scopo di test ed evitare la memorizzazione nella cache del browser. Se si desidera rendere permanente il reindirizzamento, è sufficiente modificare il flag R su R = 301.

+0

hmm ma ancora quando inserisco il sito con www safari dice che la pagina è insicura e l'opera non vuole reindirizzare alcuna opzione con o senza www a ssl, perché? –

1

Rif: Apache redirect www to non-www and HTTP to HTTPS

http://example.com

http://www.example.com

https://www.example.com

a

https://example.com

RewriteEngine On 
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^www\. [NC] 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://%1%{REQUEST_URI} [L,NE,R=301] 

Se invece di example.com si desidera l'URL predefinito per essere www.example.com, quindi è sufficiente modificare la terza e la quinta linea:

RewriteEngine On 
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://www.%1%{REQUEST_URI} [L,NE,R=301] 
Problemi correlati