htaccess を使用して 1 つを除くすべてのサブディレクトリをリダイレクトする方法
質問
.htaccess をいじってからしばらく経ちましたが、これを完全に正しく理解できないようです。たとえば example.com というサイトがあり、example.com/* を example.com/collector.html にリダイレクトしたいと考えています。 を除外する サブディレクトリ example.com/special にある URL については、作業を続けたいと考えています。
例えば:
- example.com/page.html は example.com/collector.html にリダイレクトされます。
- example.com/foo/bar は example.com/collector.html にリダイレクトされます。
- example.com/special/page.html はリダイレクトしないでください
私は次のようなことを考えていたでしょう
RedirectMatch 302 ^/[^(special)]/.* /collector.html
RedirectMatch 302 ^/[^(collector.html)/]* /collector.html
うまくいきますが、私が望んでいるようには機能しないようです。
解決
多分これ? (mod_rewriteのを必要とする)
RewriteEngine On
RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L]
他のヒント
これは、私はあなたが説明したディレクトリ構造を再作成のApache 2.2.13に私のために正常に動作します。
RedirectMatch 302 /(?!special) http://www.example.com/collector.html
括弧内のパターンは、URIは文字列「特別な」が含まれている場合は一致しませんと言っている。
またMODリライトは常に利用可能ではなく、この場合には必要ありません。
これを試してください:
RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html
RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html
たぶん...?
RewriteEngine on
RewriteRule ^(?!special) collector.html [R,NC]
所属していません StackOverflow