【问题标题】:Diggbar modrewrite- How do they pass URLs through modrewrite?Digger mod rewrite - 他们如何通过 mod_rewrite 传递 URL?
【发布时间】:2009-05-15 22:27:37
【问题描述】:

使用新的 Diggbar,您可以将http://digg.com 放在您当前所在的任何 URL 前面,它会创建一个 Digg 短 URL。我只是假设他们通过 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]

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

【问题讨论】:

    标签: php mod-rewrite


    【解决方案1】:

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

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

    【讨论】:

    • 优秀。只需添加“RewriteEngine on”就可以了!谢谢!
    【解决方案2】:

    您只是在该正则表达式中查找字母和数字,因此它不会选择冒号和斜杠。您还在示例中使用 index.php 并在 htaccess 中使用 message.php ;)

    你可能想要这样的东西:

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

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

    【讨论】:

    • 您可能需要考虑 https
    • 非常好。我从不擅长重新阅读我的帖子以获取 index.php 而不是 messages.php 之类的小问题。至于考虑 https:// 和 http://,我需要采取哪些步骤?
    • 您也许可以将正则表达式更改为 ^https?://(.+)$
    • 也看看RewriteMatch - 它可能会有所帮助
    猜你喜欢
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多