2011-10-20 11 views
0

で存在しないファイルの検索には、いくつかのテンプレートとその画像mod_rewriteを、別のフォルダ

/templates/template1/images/image1.jpg 
/templates/template1/images/header/red/new/image1.jpg 
/templates/template1/image2.jpg 
/templates/template2/images/image2.jpg 
/templates/template2/image2.jpg 

のサンプルです今度は、テンプレートが画像やファイルを見逃すことがあります。そのような場合、残りのURLを保持しながら、ユーザーを「デフォルト」のテンプレートにリダイレクトしたい場合があります。画像が見つからない場合

だから与えられた例については、利用者がこの作品を作るのが私の試みである

/templates/default/images/image1.jpg 
/templates/default/images/header/red/new/image1.jpg 
/templates/default/image2.jpg 
/templates/default/images/image2.jpg 
/templates/default/image2.jpg 

、それは

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !^/templates/default/(.*)$ 
RewriteRule ^/templates/(.*)/(.*) /templates/default/$2 [R] 
仮想ホスト・ファイルに定義されていますにリダイレクトする必要があります

今すぐ

/template1/images/image1.jpg to /templates/default/image1.jpgをリダイレクトしてから500エラーをスローします。

私はここで間違っていますか?

+0

その500エラーに関するApacheエラーログの詳細はありますか? – megaflop

+0

'^/templates /(.*)/(.*)$'に '$ /'を追加すると、 – megaflop

答えて

1

私はなぜあなたが500を取得しているのかわかりませんが、最初の.*の貪欲のため、ReqriteRuleは複数のサブディレクトリに問題があります。

/templates/template1/images/header/red/new/image1.jpgのリクエストを検討してください。このファイルが存在しない場合^/templates/(.*)/(.*)の場合、最初の(.*)は "template1/images/header/red/new"と一致し、2番目の(。*)は "image1.jpg"と一致します"/templates/default/image1.jpg"にリダイレクトされます。

より良いルール:

RewriteRule ^/templates/[^/]+/(.*)$ /templates/default/$1 [R] 

それとも、あなたはテンプレートディレクトリのみ、アンダースコアやハイフンを英数字の文字を持つことができることがわかっている場合、これはまだ良いです:

RewriteRule ^/templates/[a-zA-Z0-9_-]+/(.*)$ /templates/default/$1 [R] 

てみてくださいregexesを可能な限り具体的に保つこと。

関連する問題