【问题标题】:Intercept Angular URL with PHP first before loading webpage在加载网页之前先用 PHP 拦截 Angular URL
【发布时间】:2019-05-29 08:47:16
【问题描述】:

我们目前有一个完整的 Angular 项目在我们服务器的子目录中运行,还有一个物理“设备”使用硬编码的 URL 将用户发送到该页面。

我正在寻找某种方法来首先通过 PHP 脚本“拦截”请求(例如,不是真正的目的)查看该浏览器页面的请求“ID”参数是否启用了以下选项查看浏览器页面,或者如果它已被用户配置为返回 406 HTTP 响应(例如)。

Currently:
- ..com/app/routing-view?id=1234 => Angular view -> fetch info

Idea:
- ..com/app/routing-view?id=1234 => PHP-script -> isValid => forward to angular and do a normal 'webview' -> fetch info
- ..com/app/routing-view?id=2889 => PHP-script -> notValid => HTTP code

我想过让 .htaccess “拦截” url 并将其转发到 .php 文件。执行魔术并在那里检查,然后将浏览器转发到原始 url,但要“绕过”以前的拦截器。

这是关于我目前遇到问题的最后一部分。因为它是 Angular 并且它使用路径,所以我不能只说“好的,重定向到 index.html 而不是 index.php”,因为它需要类似于 ..com/app/routing-view?id=1234 (并且 index.html 位于/app目录。

如果可以避免的话,我不想将 PHP 代码添加到原始 Angular-index 文件中。

我是否正确地看待这个问题,还是会有其他更有效的方法来解决这个问题?

所有这一切的原因是我想(例如)向设备返回不同的 HTTP 代码或不同的标头,而不是 200 html 标头响应,即使 ID 被禁用或类似的东西。

【问题讨论】:

    标签: php angular redirect intercept


    【解决方案1】:

    通过一个简单的.htaccess、一个参数和一个“拦截器”脚本让它工作。真的很简单和基本,不知道为什么我的大脑在发布我的问题之前没有走这条路。

    1. 使用以下代码将.htaccess 添加到 Angular 应用程序的根目录。
    RewriteEngine On
    
    # Check if the request has been intercepted before (via URL parameter)
    RewriteCond %{QUERY_STRING} !(^|&)intercepted=true
    
    # .. If not, rewrite the page to use the interceptor script (only if it matches the route).
    RewriteRule ^my-route dir-of-script/extra/interceptor.php?url=https://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} [L,QSA]
    
    
    # Other stuff for rewriting the Angular routes etc
    ...
    
    
    1. 添加了一个/dir-of-script/extra/interceptor.php like-script,它从 GET 参数解析 URL、获取信息、进行检查并根据结果返回输出或重定向页面并让它通过。

    这会将..&intercepted=true 参数添加到原始URL,以便.htaccess 不会再次拦截它。

    这似乎很有效(就我的情况而言)。唯一的“缺点”是这被视为重定向,而不仅仅是允许通过时的重写。将进一步研究它,也许让 PHP 脚本“服务”Angular 内容而不是重定向到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2020-01-21
      • 2015-04-17
      • 1970-01-01
      相关资源
      最近更新 更多