js 从样式化的 URL 获取查询字符串


js get query strings from styled url

有没有办法从使用 .htaccess 更改的 URL 中获取查询字符串。例如,如果我的网址是这样的

http://mysite.com/blog.php?id=7

对于此示例,位置搜索和 PHP &_GET['id'] 也有效,但是如果我像这样更改 HTaccess,则

RewriteRule ^blog/([0-9]+)$ blog.php?id=$1 [NC,L]

网址将更改为

http://mysite.com/blog/7

现在只有 $_GET['id'] 有效,但不能使用那个 JavaScript 版本。

谢谢你,丹尼尔。

您需要

使用 location.pathname.substr(1).split('/') 手动拆分 URL。

这为您提供了一个包含各种路径段的数组。在您的情况下,它将包含['blog', '7'].