2017-06-08 10 views
0

リダイレクトしようとしています特定のURL、私のドメインからhttpへの/ admin、他のすべてはhttpsにリダイレクトする必要があります。例:.htaccess for one url

Works: 

http://example.com -> https://example.com 
http://example.com/with-url -> https://example.com/with-url 
https://example.com/admin -> http://example.com/admin 

Does not work: 

What I get (redirects from https to http, if under /admin): 
    https://example.com/admin/anything-below-here -> http://example.com/admin/anything-below-here 

What I want (to stay on https): 
    https://example.com/admin/anything-below-here -> https://example.com/admin/anything-below-here 

これは私がこれまでに得たものである:

RewriteEngine On 

# force https:// for all except /admin  
RewriteCond %{HTTPS} off 
RewriteCond %{THE_REQUEST} !/admin [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# force http:// for /admin URLs 
RewriteCond %{HTTPS} on 
RewriteCond %{THE_REQUEST} /admin [NC] 
RewriteRule^http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# all the others 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 

は、誰もが私がここで行方不明です何を教えてもらえますか?どうもありがとう!

答えて

0

あなたの正規表現は欲張りです。したがって、/admin/something/adminと一致します。したがって、adminの中にhttpsからhttpへのリダイレクトが一度入ります。

はHTTPにリダイレクトする必要があるだけ/admin/admin/を指定する代わりに/admin[/]*$を使用してみてくださいではなく、/admin/something

URIがadmin後に終了し、実際の要求は、あなたも%{REQUEST_URI}の代わりに切り替える必要がありますことを確認するため、 URIの後のデータを含む%{THE_REQUEST}(たとえば、HTTP/1.1)。

それを試してみてください。

RewriteCond %{REQUEST_URI} ^/admin[/]*$ [NC] 

あなたはそれが有効なままで/admin%{THE_REQUEST}でない場合、他の1つのチェックを変更する必要はありません。

完全なコードは、その後、次のようになります。

# force https:// for all except /admin 
RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} !^/admin[/]*$ [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# force http:// for /admin URLs 
RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} ^/admin[/]*$ [NC] 
RewriteRule^http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# all the others 
RewriteBase/      
RewriteCond %{REQUEST_FILENAME} !-f      
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 
+0

私はそのビットを追加しましたが、それはどちらか動作しません - それは私がHTTPとHTTPSの両方今でアクセス/管理しましょう。 'するRewriteCond%{THE_REQUEST}/adminに[/] * $ [NC] するRewriteCondの%{THE_REQUEST}/adminに[/] * $ [NC]' (私はわたってるしき行をスキップ! - これがすべてではありません私は今、.htaccessにいる)。 –

+0

ああ、それはあなたが%{THE_REQUEST}を使っていて、リクエストURIの後に物事があるからです(HTTP/1.1など)。$は 'admin'の直後ではありません。 – Capsule

+0

残念ながら、スラッシュの有無にかかわらず、何もしませんでした。 httpにリダイレクトされませんでした。 –