gpt4 book ai didi

php - 带有 .htaccess 的漂亮 URL

转载 作者:可可西里 更新时间:2023-10-31 22:44:31 27 4
gpt4 key购买 nike

我有一个 URL http://localhost/index.php?user=1。当我添加这个 .htaccess 文件

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/(.*)$ ./index.php?user=$1

我现在可以使用 http://localhost/user/1 链接。但是 http://localhost/index.php?user=1&action=update 我怎样才能把它变成 http://localhost/user/1/update 呢?

另外,我怎样才能使这个 url http://localhost/user/add ?谢谢。抱歉,我对 .htaccess 比较陌生。

最佳答案

如果你想转

http://www.yourwebsite.com/index.php?user=1&action=update

进入

http://www.yourwebsite.com/user/1/update

你可以使用

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=$1&action=$2

查看 PHP 中的参数:

<?php 
echo "user id:" . $_GET['user'];
echo "<br>action:" . $_GET['action'];
?>
  • .htaccess 中的括号是您可以稍后调用的组。用 1 美元、2 美元等。
  • 我添加的第一组 ([0-9]*) 意味着它将得到任何数字(1、34 等)。
  • 第二组表示任意字符(a、abc、更新等)。

在我看来,这比 (.*) 更干净、更安全,后者基本上意味着几乎所有内容都可以接受。

关于php - 带有 .htaccess 的漂亮 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25080835/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com