
モバイル バージョン ページがある Web サイトがあり、その Web サイトはリバース プロキシの背後にあるため、訪問者に表示するバージョンを決定できる条件関数が必要です。Apache 2.4 を使用していますが、ProxyPass と ProxyReverse は "If - Elseif" ステートメント内に含めることができないため、RewriteCond を試しましたが、成功しませんでした。
これが私のバーチャルホストです
<VirtualHost *:80>
ServerName MyWebsite.com
ServerAlias www.ReverseProxy/ ReverseProxy/m/
ProxyPreserveHost On
RewriteEngine On
RequestHeader set "Host" "MyWebsite.com"
#Show mobile version if visitors used mobile device
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
ProxyPass / http://MyWebsite.com:80/m/
ProxyPassReverse / http://MyWebsite.com:80/m/
#Show desktop version if not a mobile device
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]
ProxyPass / http://MyWebsite.com:80/
ProxyPassReverse / http://MyWebsite.com:80/
</VirtualHost>
上記の VirtualHost では、リバース プロキシにはモバイル バージョンのみが表示されます。これを解決するにはどうすればよいですか? 動作させるには、何を追加または変更する必要がありますか?
答え1
異なるモジュールのディレクティブを混在させて条件付きリバース プロキシを実行することはできません。
この場合、正しい方法は mod_rewrite を全面的に使用することです。
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteRule ^/(.*) http://MyWebsite.com:80/m/$1 [P,L]
ProxyPassReverse / http://MyWebsite.com:80/m/
ここで重要なのは、リダイレクトではなく mod_rewrite プロキシを作成するフラグ「P」であることに注意してください。
また、このディレクティブは、そもそもリバース プロキシしているバックエンドからのリダイレクトを処理するという、まったく別の処理を実行するため、ProxyPassReverse を使用する必要があることに注意してください。