gpt4 book ai didi

php - 简单的 mod_rewrite - 所以我再也不用考虑它了

转载 作者:行者123 更新时间:2023-12-04 07:00:28 25 4
gpt4 key购买 nike

不知道你会如何回答这个问题,但是...

每当我试图让我的 URL 看起来漂亮时,我总是会弄乱太久,这根本不值得麻烦。但如果它是一项简单的任务,最终效果是好的。

所以我想要做的是创建一个方法,它最终会实现类似......

index.php?do=user&username=MyUsername //This becomes...
/user/MyUsername //...that
index.php?do=page&pagename=customPage //And this becomes...
/page/customPage //...that
index.php?do=lots&where=happens&this=here //This also becomes...
/lots/happens/here //...that
index.php?do=this&and=that&that=this&and=some&more=too //And yes...
/this/that/this/some/more //This becomes this

所以我只是制作了一个很好的 .htacess 文件,我再也不用看它了。世界上的一切都会变得更好,因为我们有漂亮的 URL,而且我的头在制作过程中没有受伤。

最佳答案

您可以使用不同的方法将 url 放入单个参数中,并在您的应用程序中对其进行解析。

所以 apache 重写规则看起来像:

  RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

这将转换您的网址如下:
/user/MyUsername => index.php?q=/user/MyUsername
/page/customPage => index.php?q=/page/customPage
...

在您的应用程序中,您有一个 $_GET['q'] 变量,您可以将其拆分为 '/',并按顺序排列参数。在 PHP 中它会是这样的:
$args = explode('/', $_GET['q']);

$args 将是一个包含 'user'、'MyUserName' 等的数组。

这样您就不必再次触摸您的 .htaccess,只需触摸您的应用程序逻辑。

关于php - 简单的 mod_rewrite - 所以我再也不用考虑它了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1943739/

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