我正在使用 mod_rewrite 将 GET 请求重写到特定脚本。
我的目标是重定向请求:
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 和重定向客户端的 Location 标头。是否可以只提供内容,而不是重定向到内容?目标是掩盖内容的实际 URL。
当我尝试用 [L] 替换 [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
答案1
尝试这个:在你的 中RewriteRule
,将[R=302]
标志替换为[L]
。
答案2
如果您声明ServerName localhost
或ServerAlias localhost
,那么mod_rewrite
将会剥离http://localhost
并发现它可以简单地重新映射而不是重定向。
答案3
好的!不确定为什么这个有效而另一个无效;但这就是答案。如果有人能解释这个和我之前所得到的区别,我会很乐意接受他们的答案作为正确答案。
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]