【发布时间】:2020-07-23 07:42:56
【问题描述】:
如何重写此 URL:
https://example.com/illustrations.php?category=cats&cat_id=1
到:
https://example.com/category/cats
另外,我如何仍然保留cat_id 参数?
我试过了,但它不起作用:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/(.*?)/?$ /illustrations.php?category=$1 [L]
【问题讨论】:
-
“它不起作用” - 究竟发生了什么?您的指令看起来“OK”。 “仍然保留 cat_id 参数” - 你到底是什么意思?给定 URL
/category/cats,那么您唯一能做的就是在 substitution 字符串中看似硬编码cat_id参数。否则,您需要在 URL 中包含 cat_id。 “如何重写此 URL...” - 反过来,您将 that URL 重写为 this URL(这就是您在 @987654327 中所做的@ 规则)。 -
如果您想保留 cat_id 参数,它也需要成为新 URL 模式的一部分。所以也许
https://example.com/category/cats/1。没有它,就无法实现有效的重定向,因为重写模块无法以某种方式通过魔法来弥补这样的 id。 -
@MrWhite 当我添加此代码并转到illustration.php?category=cats 时,它不会更改浏览器栏中的网址。
-
@arkascha 我将如何实现这一目标?
-
您在应用程序/站点中链接到哪个 URL?
标签: php .htaccess url-rewriting