Dilbar mod_rewrite-他们如何通过mod_rewrite传递URL?

时间:2009-05-06 19:40:24

标签: php mod-rewrite

使用新的Diggbar,您可以将http://digg.com放在您当前所在的任何网址前面,它会创建一个Digg短网址。我只是假设他们通过modrewrite做到这一点(虽然我不确定,因为我是新手)。

这是怎么做到的?在我尝试使用我正在处理的网站时,我觉得它在爆炸。

我希望能够做到以下几点:

http://example.com/http://stackoverflow.com/question/ask

并有一个modrewrite,允许它转到

http://example.com/index.php?url=http://stackoverflow.com/question/ask

但是当我使用这个modrewrite时:

RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ /message.php?id=$1 [L]

它不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:5)

您必须从request line获取值,因为Apache会删除空路径段。最初请求的URI路径/http://foobar/变为/http:/foobar/。但请求行(THE_REQUEST)保持不变:

RewriteCond %{THE_REQUEST} ^GET\ /(https?://[^\s]+)
RewriteRule ^https?:/ index.php?url=%1 [L]

答案 1 :(得分:1)

你只是在正则表达式中查找字母和数字,所以它不会拾取冒号和斜杠。您还在示例中使用了index.php,在htaccess中使用了message.php;)

你可能想要这样的东西:

RewriteEngine on
RewriteRule ^http://(.+)$ /index.php?url=$1 [L]

这确保您只能在此处捕获网址,并且您仍然可以拥有常规网页! (想想如果你去example.com/index.php会发生什么,你最终会陷入无限循环。)

相关问题