传递“取消关注”使用 Apache 的 mod_rewrite 标记 GET 参数

发布于 2024-09-29 22:58:27 字数 975 浏览 0 评论 0原文

编辑 @mootinator 在这方面做了出色的工作。请参阅下面他的回答。

由于我不清楚指定确切的 URI,它仍然可能对正在寻找类似内容的人有所帮助。我的 URI 更像是: 9/Here-is-some-text/unwatch

...在这种情况下,您需要看起来更像这样的 mod_rewrite 规则:

RewriteEngine on
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]

Pretty对@mootinator的答案进行了小修改,但我花了几分钟自己弄清楚。可以帮助某人节省后续时间。


我正在尝试执行 mod_rewrite 来传递 GET 参数,这是一种布尔标志,以便每当有人访问 URI/unwatch ,您可以拉出&unwatch=1

我已经有了这条规则:

RewriteRule ^(\d+)/* index.php?id=$1 [L]

我希望有另一个规则可以执行类似的操作:

RewriteRule ^(\d+)/unwatch index.php?id=$1&unwatch=1 [L]

“目录”./unwatch 根本不是目录。目录结构如下所示:

./index.php
./.htaccess

关于如何执行此操作有什么想法吗?

EDIT @mootinator has done awesome work with this. See his answer below.

Since I wasn't clear in specifying my exact URI, it may still help people who are looking for something similar. My URI is more like: 9/Here-is-some-text/unwatch

...in which case you need mod_rewrite rules that look a little more like this:

RewriteEngine on
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]

Pretty minor modification to @mootinator's answer, but took me a few minutes to figure out on my own. May help someone save time on down the line.


I'm trying to do a mod_rewrite to pass in a GET parameter, a sort of boolean flag so that whenever someone goes to URI/unwatch, you can pull out &unwatch=1.

I've got this rule already:

RewriteRule ^(\d+)/* index.php?id=$1 [L]

I would like to have another rule that does something like this:

RewriteRule ^(\d+)/unwatch index.php?id=$1&unwatch=1 [L]

The "directory" ./unwatch is not a directory at all. The directory structure looks like:

./index.php
./.htaccess

Any thoughts on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2024-10-06 22:58:27
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]

QSA 代表查询字符串追加。这样用户传递的参数就不会被删除。

如果我缺少一些涉及绕过不是实际目录的 url 的内容,请查看如何 CakePHP 做到了。

使用以下 .htaccess 进行工作演示此处

<IfModule mod_rewrite.c>
   RewriteBase /mod_rewrite
   RewriteEngine on
   RewriteRule ^([0-9]+)/?$ index.php?id=$1 [QSA,L]
   RewriteRule ^([0-9]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]
</IfModule>

结果是我在 [QSA, L] 导致了 500 错误,而 \d 的行为不符合我的预期。

RewriteRule ^([0-9]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]

QSA stands for Query string append. This way parameters passed by the user don't get erased.

If there's something I'm missing involving getting around the url not being an actual directory, check out how CakePHP does it.

Working demo here using the following .htaccess:

<IfModule mod_rewrite.c>
   RewriteBase /mod_rewrite
   RewriteEngine on
   RewriteRule ^([0-9]+)/?$ index.php?id=$1 [QSA,L]
   RewriteRule ^([0-9]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]
</IfModule>

Turns out the space I had in [QSA, L] was causing the 500 error, and \d wasn't behaving as I expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文