【问题标题】:Redirecting .htaccess from angular.js route从 angular.js 路由重定向 .htaccess
【发布时间】:2014-12-08 07:08:20
【问题描述】:

我正在使用 angular.js 应用程序通过外部 API 获取内容。一切都很好,除了元数据和 opengraph 值。 由于我们无法重写所有元数据,我们决定使用一些重定向来在带有元数据的特定页面上发送爬虫。我们使用这种方法:https://github.com/michaelbromley/angular-social-demo 一切似乎都有效,但是当我们需要从 .htaccess 文件中获取“路由”参数时,我们什么都没有。我们可以在特定页面发送FB爬虫,但没有信息。

这是我们的 .htaccess :

RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule the-page-with-angular-app.html(.*)$ http://www.oursite.net/some/directory/static-page.php?id=$1 [P]

这是我们的网址:

http://www.oursite.net/the-page-with-angular-app.html#/someInfo/3437/46

请注意,我们的 angular.js 应用在 URL 中需要“#”(由于某些原因我们不能使用 html5mode)。

我们希望至少重定向到页面:oursite.net/some/directory/static-page.php?id=someInfo/3437/46,即使重定向到oursite.net/some/directory/会更好static-page.php?id1=someInfo&id2=3437&id3=46

目前,我们可以重定向到 oursite.net/some/directory/static-page.php(如果我们在 .htaccess 中强制它,我们可以传递 GET id)。重写条件效果很好。

感谢您的帮助,

【问题讨论】:

  • 我对带有 '#' 和 .htaccess 重定向的角度路由的经验是它们无法正常工作......
  • 让我更具体一点。我不想在 Angular 应用程序中重定向“普通”用户,我希望让爬虫在 php 脚本中重定向他们,只返回一些信息(例如元、开放图元、推特卡等)。我需要保留一些变量才能这样做。我可以重定向,但我暂时失去了变量。

标签: javascript php angularjs .htaccess redirect


【解决方案1】:

我们采用另一种方法来成功。

我们在 angular.js 应用程序中构建自定义共享 URL(如 ourdomain.com/one-folder/to-static.php?id1=somme&id2=stuff)

我们使用这些 URL 在社交网络(FB、Twitter 等)中分享内容。我们构建了自定义 to-static.php 来处理元数据(推特卡、开放图等)。

我们可以使用 .htaccess 将“普通”用户从 angular.js 深层链接中的“to-static.php”脚本重定向。像这样的东西(但我不使用那个):

RewriteCond %{HTTP_USER_AGENT} !(facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule static.php?id1=(.*)&id2=(.*)$ http://www.oursite.net/the-page-with-angular-app.html#/$1/$2 [P]

我决定在 PHP 脚本中进行测试并使用 PHP 方法重定向“普通”用户(我使用这段代码来检测爬虫):https://stackoverflow.com/a/20949242/2207250

我们最终不使用 .htaccess 方法,因为我们的客户安装(第 50 个网站上的相同 angular.js 应用程序:joomla 网站、wordpress 网站、自定义网站、移动应用程序等)要求修改 htaccess 很棘手文件(并非不可能,但如果我们能做其他方式,那就更好了)。

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 2023-03-17
    • 2021-11-06
    • 1970-01-01
    • 2015-11-24
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多