.htaccessで条件付きでルールをスキップするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1035953

質問

" _js"、" _css"、" _img"などを含まないすべてのURLをディスパッチスクリプトにリダイレクトしたい。どういうわけか機能しません。 たとえば、/ _ js /フォルダー内のすべてのファイルにアクセスできません(つまり、そのフォルダーにある物理ファイルに到達する代わりに、index.phpに送信されます)。

これは私のhtaccessです:

IndexIgnore *
Options +FollowSymLinks
RewriteEngine on

# if the following conditions are met, SKIP the rewriteRules.

RewriteCond %{REQUEST_URI} ^/(_admin/¦_css/¦_js/¦_img/)
RewriteRule . - [S=9]


# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://%{HTTP_HOST}/$1/?%{QUERY_STRING} [NC,R,L]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&%{QUERY_STRING} [NC,L]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1&%{QUERY_STRING}  [NC,L]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L]
# ONE PARAM
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=$1&%{QUERY_STRING} [NC,L]

編集:

フォルダ構造に注意してください。問題の原因になりますか?

「v1」があります;および「v2」フォルダー構造。この.htaccessは" v2"にあります。フォルダ。 1つ上のレベルでは、すべてのリクエストを「v2」にリダイレクトする.htaccessがあります。

root 
  L.htaccess << dispatch between v1 and v2 folders 
  L v1 L v2 L.htaccess << the .htaccess code posted above 
  L _admin L all my website files & folders
役に立ちましたか?

解決

| 垂直線ではなく、間違った文字 ¦ 壊れたバー、U + 00A6)を使用しています、U + 007C)、および REQUEST_URI のパターンが間違っています。

RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]

または v2 ディレクトリの.htaccessファイルの場合:

RewriteRule ^_(admin|css|js|img)/ - [S=9]

他のヒント

実際の問題を修正する場合としない場合があるランダムな修正をいくつか示しますが、完全に明確ではなく(私のコメントを参照)、サーバーを制御してRewriteLogを有効にすると役立つ場合があります:

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

ただし、これをメインサーバー構成に配置する必要があります。

とはいえ、2つの主な問題:

  • QSAフラグの使用不足(実際には関係ありません)
  • スラッシュの過度の使用(おそらく実際の問題に関連しています。スキップルールの新しいバージョンを参照してください)

ここで変更されたファイルになります

IndexIgnore *
Options +FollowSymLinks
RewriteEngine on

# if the following conditions are met, SKIP the rewriteRules.

RewriteCond %{REQUEST_URI} ^(_admin¦_css¦_js¦_img)
RewriteRule . - [L]


# Externally redirect to add missing trailing slash. Not really needed, AFAICS
# RewriteRule ^(([^/]+/)*[^/]+)$ http://%{HTTP_HOST}/$1/ [NC,R,L,QSA]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6 [NC,L,QSA]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5 [NC,L,QSA]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4 [NC,L,QSA]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3 [NC,L,QSA]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/?$ index.php?section=downloads&item=$1  [NC,L,QSA]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2 [NC,L,QSA]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([^/]+)/?$ index.php?tag=$1 [NC,L,QSA]
# ONE PARAM
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]

編集:説明されているフォルダー構造を前提として、最初にv2 .htaccessに次を追加してみてください:

RewriteBase /

RewriteLogを使用できるかどうかはまだ説明していません(できないと思います)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top