PHP 和 Apache:将 php 参数隐藏为目录;未访问第二个参数


PHP and Apache: hiding php parameters as directories; second parameter not accessed?

我目前正在使用apache代码:

RewriteEngine On
RewriteBase /
RewriteRule ^index'.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Categories + Page:
RewriteRule ^([a-z'-]+)/([a-z'-]+)$ /index.php?category=$1&page=$2
RewriteRule ^([a-z'-]+)/([a-z'-]+)/$ /index.php?category=$1&page=$2
# Categories:
RewriteRule ^([a-z'-]+)$ /index.php?category=$1
RewriteRule ^([a-z'-]+)/$ /index.php?category=$1

这将我的网址从:示例.com?类别=软件&页面=移动到example.com/software/mobile/

这工作正常,php 文件能够 $_GET['category'] 罚款;但是,由于某种原因,它无法识别 $_GET['page'];它只会导致它每次都是空的。

在 Apache 代码方面,我不是最好的,所以有人能告诉我我哪里出错了?

我用于 php 获取变量的代码很简单:

$category = $_GET['category'];
$page = $_GET['page'];

此外,如果有人可以帮助我优化它,以便它可以适用于任意数量的参数,那就更好了。

谢谢!

RewriteRule ^([a-z'-]+)/([a-z'-]+)$ /index.php?category=$1&page=$2 [PT,L,QSA]
RewriteRule ^([a-z'-]+)/([a-z'-]+)/$ /index.php?category=$1&page=$2 [PT,L,QSA]
# Categories:
RewriteRule ^([a-z'-]+)$ /index.php?category=$1 [PT,L,QSA]
RewriteRule ^([a-z'-]+)/$ /index.php?category=$1 [PT,L,QSA]

添加 QSA 可能会有所帮助

当替换 URI 包含查询字符串时,默认 重写规则的行为是丢弃现有的查询字符串,并且 将其替换为新生成的。使用 [QSA] 标志的原因 要组合的查询字符串。

请考虑以下规则:

重写规则/pages/(.+)/page.php?page=$1 [QSA]

使用 [QSA] 标志,将映射对/pages/123?one=two 的请求 到/page.php?page=123&one=two。如果没有 [QSA] 标志,同样 请求将映射到/page.php?page=123 - 即现有的 查询字符串将被丢弃。