我正在使用mod_rewrite重写GET请求到一个特定的脚本。
我的目标是redirect请求:
http://localhost/Work/dbUI/pageElements/Dialog/Contact.php?id=138750
至
http://localhost/Work/dbUI/pageElements/Dialog.php?dialog=Contact.php&id=138750
我在用着:
RewriteEngine On RewriteLog "/var/log/rewrite" RewriteLogLevel 3 RewriteCond %{THE_REQUEST} /Work/dbUI/pageElements/Dialog/ RewriteCond %{QUERY_STRING} id=([^&?]*) RewriteRule (.*) http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=%1 [R=302]
它工作得很好; 但是当我提出请求时,我得到了以下头文件(这是一个cURL例程的输出):
* About to connect() to localhost port 80 (#0) * Trying 127.0.0.1... * connected * Connected to localhost (127.0.0.1) port 80 (#0) > GET /Work/dbUI/pageElements/Dialog/Contact.php?id=138750 HTTP/1.1 Host: localhost Accept: */* < HTTP/1.1 302 Found < Date: Tue, 09 Nov 2010 21:37:54 GMT < Server: Apache/2.2.16 (Fedora) < Location: http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750 < Content-Length: 338 < Connection: close < Content-Type: text/html; charset=iso-8859-1
正如你所看到的,我得到一个302和一个位置头redirect客户端。 是否可以简单地提供内容而不是redirect到内容? 目标是掩盖内容的实际URL。
当我尝试用[L]添加replace[R = 302]时,在重写日志中得到以下输出:
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) init rewrite engine with requested uri /Work/dbUI/pageElements/Dialog/Contact.php 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) applying pattern '(.*)' to uri '/Work/dbUI/pageElements/Dialog/Contact.php' 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) rewrite '/Work/dbUI/pageElements/Dialog/Contact.php' -> 'http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750' 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) split uri=http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750 -> uri=http://localhost/Work/dbUI/pageElements/Dialog, args=dialog=Contact.php&id=138750 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) implicitly forcing redirect (rc=302) with http://localhost/Work/dbUI/pageElements/Dialog 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) escaping http://localhost/Work/dbUI/pageElements/Dialog for redirect 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) redirect to http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750 [REDIRECT/302] 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) init rewrite engine with requested uri /Work/dbUI/pageElements/Dialog 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) applying pattern '(.*)' to uri '/Work/dbUI/pageElements/Dialog' 127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) pass through /Work/dbUI/pageElements/Dialog
试试这个:在你的RewriteRule ,用[L]replace[R=302]标志。
如果声明ServerName localhost或ServerAlias localhost ,则mod_rewrite会剥离http://localhost并查看它可以简单重新映射而不是redirect。
好! 不知道为什么这个工程,另一个没有; 但是这是答案。 如果有人能够解释这个和我之前所做的事情之间的区别,我很乐意接受他们的正确答案。
RewriteEngine On RewriteLog "/var/log/rewrite" RewriteLogLevel 3 RewriteCond %{THE_REQUEST} /Work/dbUI/pageElements/Dialog/ RewriteCond %{QUERY_STRING} id=([^&?]*) RewriteRule ^/Work/dbUI/pageElements/Dialog/(.*) /Work/dbUI/pageElements/Dialog?dialog=$1&id=%1 [L]