0

私のapacheサイト構成ファイルには、次のものがあります。書き換えルールは正常に機能しますが、このルールは、書き換え条件が修正されるはずの既存のディレクトリに適用されます。これが私の書き直しスクリプトです:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]

したがって、example.com/usernameうまく機能し、を使用しますtest.phpが、ファイルを含む既存のサブディレクトリに入るindex.phpと、別名、example.com/myDirectory/test.phpを使用します。私のスクリプトの何が問題になっているのでしょうか。ありがとう。

myDirectory以下は、既存のディレクトリであるリクエストの書き換えログです。

(2) init rewrite engine with requested uri /myDirectory/
(1) pass through /myDirectory/
(3) [perdir /var/www/] strip per-dir prefix: /var/www/myDirectory/ -> myDirectory/
(3) [perdir /var/www/] applying pattern '^tag/([a-zA-Z0-9-]+)/?$' to uri 'myDirectory/'
(3) [perdir /var/www/] strip per-dir prefix: /var/www/myDirectory/ -> myDirectory/
(3) [perdir /var/www/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'myDirectory/'
(2) [perdir /var/www/] rewrite 'myDirectory/' -> 'test.php?n=myDirectory'
(3) split uri=test.php?n=myDirectory -> uri=test.php, args=n=myDirectory

bananasそして、これがリライトを使用することを意図した(そして実際に実行する)リクエストのログです:

(2) init rewrite engine with requested uri /bananas
(1) pass through /bananas
(3) [perdir /var/www/] strip per-dir prefix: /var/www/bananas -> bananas
(3) [perdir /var/www/] applying pattern '^tag/([a-zA-Z0-9-]+)/?$' to uri 'bananas'
(3) [perdir /var/www/] strip per-dir prefix: /var/www/bananas -> bananas
(3) [perdir /var/www/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'bananas'
(2) [perdir /var/www/] rewrite 'bananas' -> 'test.php?n=bananas'
(3) split uri=test.php?n=bananas -> uri=test.php, args=n=bananas
4

2 に答える 2

0

だから、私はすべてのコードを投稿する必要がありました。そこに2番目の書き換えルールがありました。だからそれは次のように見えました:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^tag/([a-zA-Z0-9-]+)/?$ tag/index.php?t=$1 [L]
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]

私が知らなかったのは、すべてのルールの前にRewriteCondを適用する必要があるということです。私はここでその答えを見つけました。

したがって、正しい方法は次のようになります。

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^tag/([a-zA-Z0-9-]+)/?$ tag/index.php?t=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]
于 2012-12-11T20:03:43.353 に答える
0

<Directory>ブロック内にこれらのディレクティブがあるようです。これらのルールを逐語的にローカルのApache構成に入れると、次のようになります。

<Directory "/srv/http">
        RewriteEngine On
        RewriteCond %{SCRIPT_FILENAME} !-d
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]
</Directory>

そしてそれRewriteLogLevel>=4であることを確認してください。

RewriteLogLevel 4

リクエストすると、ログに次のように表示されます/myDirectory

(2) init rewrite engine with requested uri /myDirectory/
(1) pass through /myDirectory/
(3) [perdir /srv/http/] strip per-dir prefix: /srv/http/myDirectory/ -> myDirectory/
(3) [perdir /srv/http/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'myDirectory/'
(4) [perdir /srv/http/] RewriteCond: input='/srv/http/myDirectory/' pattern='!-d' => not-matched
(1) [perdir /srv/http/] pass through /srv/http/myDirectory/

一方、存在しないディレクトリを要求すると、次のようになります。

(2) init rewrite engine with requested uri /notmyDirectory/
(1) pass through /notmyDirectory/
(3) [perdir /srv/http/] add path info postfix: /srv/http/notmyDirectory -> /srv/http/notmyDirectory/
(3) [perdir /srv/http/] strip per-dir prefix: /srv/http/notmyDirectory/ -> notmyDirectory/
(3) [perdir /srv/http/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'notmyDirectory/'
(4) [perdir /srv/http/] RewriteCond: input='/srv/http/notmyDirectory' pattern='!-d' => matched
(4) [perdir /srv/http/] RewriteCond: input='/srv/http/notmyDirectory' pattern='!-f' => matched
(2) [perdir /srv/http/] rewrite 'notmyDirectory/' -> 'test.php?n=notmyDirectory'

したがって、ルールはあなたが望むことを正確に実行しています。

4まで上げてみてくださいRewriteLogLevel(それより低い値は処理を表示しませんRewriteCond)。

于 2012-12-11T18:50:51.580 に答える