5

codeigniter でサイトを構築しています。.htaccess ファイルに一連の書き換え条件とルールがあります。ルールの最初のセットは、URI の最初のセグメントに応じて SSL をオンまたはオフにします。

その後、再びループし、一致が見つかった場合は、ページを適切にリダイレクトします。一致するものがなく、URI がリストされている文字列のいずれでも始まらない場合は、別のページにリダイレクトされます。条件が満たされない場合は、インデックス ページに移動します。

問題は、SSL のオンとオフを切り替える最初の一連のルールにあります。uri が admin または secure で始まる必要があることを指定したいと思います。しかし、文字列の先頭に ^ を追加すると、すべてが壊れます。ここに私が持っているものがあります:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/?(admin|secure)
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} !^/?(admin|secure)
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

...specific rewrites here
RewriteRule ^secure-form1$ secure/contract/secure-form1 [L]
RewriteRule ^secure$ secure/article/secure [L]

RewriteCond %{HTTP_HOST} ^www.example.com$ 
RewriteCond %{REQUEST_URI} !^/(admin|form|secure|page)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/page/article/$1 [L]

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

最初の 2 つの書き換え条件 (https のオンとオフ) で ^ 記号を保持すると、 http://www.example.com/secureがhttps://www.example.com/index.php/secure/article/ に書き換えられ ます。 最後の書き換えルールであるセキュア。URLは実際にはブラウザでこれに変わります。

最初の 2 つの書き換え条件から ^ 記号を取り除くと、右のページに移動します。しかし、SSL を使用してはならない URI の途中 (およびスラッシュの後に) に「セキュア」があるページが他にもあるため、URI の先頭を指定する必要があります。

私はこれを理解することはできません。

4

2 に答える 2

11

これを試してください:

RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(.*)$ 
RewriteRule ^/?(admin|secure)$ https://%1/$1 [R=301,L]
于 2012-09-17T19:53:31.983 に答える
8

Old question, I know, but there's another solution that might work better if you really want to stick with the %{REQUEST_URI} condition:

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/?(admin|secure)$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The key difference versus the OP is the inclusion of /? to check for the presence of a forward slash at the beginning of the URI. AFAIK, different Apache installations may or may not include the opening slash in %{REQUEST_URI}.

One benefit of doing it this way is that you can apply the rule to multiple conditions:

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/?admin$ [OR]
RewriteCond %{REQUEST_URI} ^/?secure$ [OR]
RewriteCond %{REQUEST_URI} ^/?top-secret$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

For the way I think, this is easier to work with than a long chain of pipe-delimited strings. This comes at the cost of a potential efficiency loss versus a single regex, but there are some other improvements you can make to offset this:

  • Use the lexographically equal operator !=on. If you just use off it gets treated as a regex.

  • Eliminate the RewriteRule pattern, replacing it with a single caret, then use the environment vars %{HTTP_HOST} and %{REQUEST_URI}. This saves the overhead of a longer regex pattern as well as the backrefs.

于 2014-01-09T18:04:42.363 に答える