【问题标题】:How to capture the requested url and pass its substring to some other link in .htaccess如何捕获请求的 url 并将其子字符串传递给 .htaccess 中的其他链接
【发布时间】:2020-04-03 05:52:47
【问题描述】:

我正在对 .htacess 文件进行一些更改,我想捕获请求的 url,创建它的子字符串并将其传递给第三方 url。 例如

我正在开发 ubuntu,所以目前我的 .htaccess 文件在文件夹 test 中。因此,当我访问 localhost/test/http://www.facebook.com 时,它会将我带到 service.prerender.io/test/http://www.facebook.com 但我想消除“测试”并转发像 service.prerender.io/http://www.facebook.com 这样的请求

这是我的 .htaccess 文件的代码

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteRule ^(.*)$ http://service.prerender.io%{REQUEST_URI} [L,QSA]
</IfModule>

【问题讨论】:

    标签: apache .htaccess http


    【解决方案1】:

    您可以使用RewriteCond %{REQUEST_URI} 设置捕获组,而不是重写为http://service.prerender.io%{REQUEST_URI},您可以重写为类似http://service.prerender.io/%1 的内容。

    这是一个例子(未经测试):

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/script.php
    RewriteCond %{REQUEST_URI} ^/?test/([.]+) [NC]
    RewriteRule ^(.*)$ http://service.prerender.io/%1? [L,QSA]
    </IfModule>
    

    【讨论】:

    • 仍然没有运气,我用相同的请求尝试了你的代码,即localhost/test/http://www.facebook.com,但它把我带到了service.prender.io/m。我不知道这是怎么回事
    • 还没有工作,现在它说 url not found 它没有加载任何东西 ERROR:404.
    • @Utkarsh 我编辑了你可以再试一次。
    【解决方案2】:

    您要捕获的不是直接请求的 uri,而是其中的一部分。你可以从重写规则中得到它

    尝试关注

    RewriteRule ^test/(.*)$ http://service.prerender.io/$1? [L,QSA]
    

    如果您正在访问类似的页面,

    https://www.example.com/thisdir/thisfile.php
    

    然后你会得到以下请求 uri 和脚本 uri

    REQUEST_URI: /thisdir/thisfile.php
    
    SCRIPT_URI: https://www.example.com/thisdir/thisfile.php
    

    https://github.com/prerender/prerender-apache 是关于 htaccess 和 prerender 的更多信息

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2012-12-25
      相关资源
      最近更新 更多