php - 使用 Apache 的 mod_rewrite 传入 "unfollow"标志 GET 参数

标签 php apache mod-rewrite get

编辑 @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]

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


我正在尝试执行 mod_rewrite 来传递 GET 参数,这是一种 bool 标志,以便每当有人访问 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

关于如何做到这一点有什么想法吗?

最佳答案

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

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

如果我遗漏了一些涉及绕过不是实际目录的 URL,请查看如何 CakePHP做到了。

工作演示 here使用以下 .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 的行为与我预期的不同。

关于php - 使用 Apache 的 mod_rewrite 传入 "unfollow"标志 GET 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4061851/

相关文章:

php - 根据是否设置了 $bar 回显 foo

php - 允许Apache写入本地同步的docker卷

node.js - ubuntu 16.04 上的 Nodejs - 正确安装 browser-sync gulp

.htaccess - 为什么在 .htaccess 中使用 [QSA] 时 $_POST 为空?

apache - 301 将 index.html 重定向到/或/index.php

mod-rewrite - Lighttpd 配置,. (点)在我的查询字符串中导致 404

php - 使用 smarty 获取数组中的值计数

php - 在 HTML 文本字符串中插入隐藏字符?

php - 从我的 SELECT 语句中只返回 4 个 ID

php - .htaccess 指向页面的 php 版本(如果存在)