2016-11-29 5 views
1

私は説明できない奇妙な動作に遭遇しました。 HTTPリクエストをHTTPSにリダイレクトする必要があります。私は次のコードを使用しました:RewriteRuleがファイルにマッピングする代わりにURLを変更する

RewriteEngine On 
RewriteBase/

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

RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# The query string in the rewrite is for testing purposes 
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&http=%{HTTPS} [L] 

これまでのところ、それは動作します。その後、私はHTTPする単一のページを必要とするので、私はいくつかの書き換え条件を追加しました:さて、ここで何が起こっているのか

RewriteEngine On 
RewriteBase/
RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} ^/not-https 
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L] 

RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} !^/not-https 
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L] 

RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&https=%{HTTPS} [L] 

。何らかの理由で、ページにアクセスすると、/index.php?url=not-https&/not-https&https=offにリダイレクトされます。

次に、リダイレクト/表示されたURLが続くGETリクエストのマップです。

GET: http://example.com/test 
    -> https://example.com/test with proper $_GET 

GET: http://example.com/test.jpg 
    -> https://example.com/test.jpg with no $_GET (file exists) 

GET: https://example.com/not-https 
    -> http://example.com/not-https 
    -> http://example.com/index.php?url=not-https&/not-https&https=off 

私の質問は、なぜnot-https表示されたURL(およびそのための、めちゃくちゃに自分のアプリケーションを)変更するんでしょうか?

答えて

1

REQUEST_URI変数の値が条件!^/non-httpsは第二の規則で成功になり、それがそのルールを実行させる最後のルールに/index.php?...に変化しているので、それが起こっています。

はこれにあなたの第一2つのルールを変更し

RewriteCond %{HTTPS} on 
RewriteCond %{THE_REQUEST} \s/+not-https [NC] 
RewriteRule^http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 

RewriteCond %{HTTPS} off 
RewriteCond %{THE_REQUEST} !\s/+not-https [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 

THE_REQUEST変数REQUEST_URIとは異なり、それが他の内部の書き換えを実行した後の値です変更されません。

+1

ありがとう、それはすべてを解決しました。 'REQUEST_URI'は何とか変わっていたと私は推測しましたが、ドキュメントでは何も見つかりませんでした。さらに、 '.htaccess'ファイルをデバッグするのが非常に難しいと感じています... –

関連する問題