Por que dois padrões (/.*) e (. *) correspondem a strings diferentes?

1

Os dois padrões (/.*) e (.*) não devem corresponder à mesma sequência? Minha verdadeira questão é: onde o "abc" foi? Algo estranho parece estar acontecendo dentro do mecanismo mod_rewrite ...

Dado este arquivo .htaccess em www/dir/ :

Options +FollowSymlinks
RewriteEngine on
RewriteRule (/.*) print_url_args.php?result=$1

Uma solicitação para http://localhost/dir/abc/123 / resulta em:

  • result ($ 1)="/ 123 /"
  • $ _ REQUEST_URI="/ dir / abc / 123 /"

Se o / for removido do padrão, como RewriteRule (.*) print_url_args.php?result=$1

O mesmo pedido para 1http: // localhost / dir / abc / 123/1 resulta em:

  • result ($ 1)="print_url_args.php"
  • $ _ REQUEST_URI="/ dir / abc / 123 /"

update: registrou o registro de regravação.
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (3) [perdir C:/db/www/dir/] add path info postfix: C:/db/www/dir/abc -> C:/db/www/dir/abc/123/
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (3) [perdir C:/db/www/dir/] strip per-dir prefix: C:/db/www/dir/abc/123/ -> abc/123/
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (3) [perdir C:/db/www/dir/] applying pattern '(/.*)$' to uri 'abc/123/'
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (2) [perdir C:/db/www/dir/] rewrite 'abc/123/' -> 'print_url_args.php?result=/123/'
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (3) split uri=print_url_args.php?result=/123/ -> uri=print_url_args.php, args=result=/123/
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (3) [perdir C:/db/www/dir/] add per-dir prefix: print_url_args.php -> C:/db/www/dir/print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (2) [perdir C:/db/www/dir/] strip document_root prefix: C:/db/www/dir/print_url_args.php -> /dir/print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#23cd4a8/initial] (1) [perdir C:/db/www/dir/] internal redirect with /dir/print_url_args.php [INTERNAL REDIRECT]
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#43833c8/initial/redir#1] (3) [perdir C:/db/www/dir/] strip per-dir prefix: C:/db/www/dir/print_url_args.php -> print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#43833c8/initial/redir#1] (3) [perdir C:/db/www/dir/] applying pattern '(/.*)$' to uri 'print_url_args.php'
127.0.0.1 - - [15/Feb/2011:14:21:51 +0900] [localhost/sid#1333140][rid#43833c8/initial/redir#1] (1) [perdir C:/db/www/dir/] pass through C:/db/www/dir/print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (3) [perdir C:/db/www/dir/] add path info postfix: C:/db/www/dir/abc -> C:/db/www/dir/abc/123/
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (3) [perdir C:/db/www/dir/] strip per-dir prefix: C:/db/www/dir/abc/123/ -> abc/123/
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (3) [perdir C:/db/www/dir/] applying pattern '(.*)$' to uri 'abc/123/'
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (2) [perdir C:/db/www/dir/] rewrite 'abc/123/' -> 'print_url_args.php?result=abc/123/'
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (3) split uri=print_url_args.php?result=abc/123/ -> uri=print_url_args.php, args=result=abc/123/
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (3) [perdir C:/db/www/dir/] add per-dir prefix: print_url_args.php -> C:/db/www/dir/print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (2) [perdir C:/db/www/dir/] strip document_root prefix: C:/db/www/dir/print_url_args.php -> /dir/print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23bf470/initial] (1) [perdir C:/db/www/dir/] internal redirect with /dir/print_url_args.php [INTERNAL REDIRECT]
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23fda10/initial/redir#1] (3) [perdir C:/db/www/dir/] strip per-dir prefix: C:/db/www/dir/print_url_args.php -> print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23fda10/initial/redir#1] (3) [perdir C:/db/www/dir/] applying pattern '(.*)$' to uri 'print_url_args.php'
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23fda10/initial/redir#1] (2) [perdir C:/db/www/dir/] rewrite 'print_url_args.php' -> 'print_url_args.php?result=print_url_args.php'
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23fda10/initial/redir#1] (3) split uri=print_url_args.php?result=print_url_args.php -> uri=print_url_args.php, args=result=print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23fda10/initial/redir#1] (3) [perdir C:/db/www/dir/] add per-dir prefix: print_url_args.php -> C:/db/www/dir/print_url_args.php
127.0.0.1 - - [15/Feb/2011:14:24:54 +0900] [localhost/sid#1333140][rid#23fda10/initial/redir#1] (1) [perdir C:/db/www/dir/] initial URL equal rewritten URL: C:/db/www/dir/print_url_args.php [IGNORING REWRITE]
    
por Leftium 15.02.2011 / 03:19

2 respostas

2

Quando você estiver em .htaccess context (/.*) nunca corresponderá à URL completa, em os documentos :

In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).

e

Per-directory Rewrites

...

The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

    
por 15.02.2011 / 04:06
0

Sua pergunta me ajudou a resolver um problema muito parecido!

Eu não tenho acesso para reescrever registros em meu ambiente de hospedagem (godaddy), então fiquei totalmente perplexo com a "segunda" reescrita, que seus logs explicam claramente. A partir daí, não foi muito difícil entender que a chave para fazer isso funcionar é que as reescritas recursivas precisam ser interrompidas de alguma forma.

Existe uma ótima solução para esse problema aqui (via usuário do servidor de defesa Dave Drager answer para uma pergunta semelhante).

Tente isto:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{ENV:
REDIRECT_X} =""
RewriteRule (.*) print_url_args.php?result=$1 [E=X:1,L]
    
por 01.11.2011 / 17:26