【问题标题】:How to Rewrite url using RewriteRule如何使用 RewriteRule 重写 url
【发布时间】:2021-12-30 14:40:37
【问题描述】:
我无法弄清楚为什么重写规则不起作用。我想重写localhost/admin/update.php?id=1,其中id是动态的,基于我单击以从数据库中获取数据到localhost/admin/update/1的用户
这是我尝试的 .htaccess 文件:
RewriteEngine on
RewriteRule ^update update.php [NC,L]
RewriteRule ^update/([0-9]+)$ update.php?id=$1 [NC,L]
【问题讨论】:
标签:
apache
.htaccess
mod-rewrite
【解决方案1】:
RewriteRule ^update update.php [NC,L]
RewriteRule ^update/([0-9]+)$ update.php?id=$1 [NC,L]
您需要删除第一条规则并确保禁用 MultiViews。您的第一条规则阻止了第二条规则(并且从问题中所述的标准没有任何目的)。例如:
Options -MultiViews
RewriteEngine On
RewriteRule ^update/([0-9]+)$ update.php?id=$1 [NC,L]
这假定.htaccess 文件位于/admin 子目录内(在 cmets 中暗示)。
我想重写localhost/admin/update.php?id=1,其中id是动态的,基于我单击以从数据库中获取数据到localhost/admin/update/1的用户
请注意,您的 描述 与上述指令的作用(以及您应该做什么)完全相反。此规则将请求从 /admin/update/1 重写为 /admin/update.php?id=1。您应该在您的应用程序中链接到 /admin/update/1。