gpt4 book ai didi

php - mod_rewrite、php 和 .htaccess 文件

转载 作者:可可西里 更新时间:2023-11-01 13:42:24 26 4
gpt4 key购买 nike

我正在编写一个小型 CMS,以便更好地了解它们的工作原理并学习一些有关 PHP 的新知识。然而,我遇到了一个问题。

我想使用 mod_rewrite(尽管如果有人有更好的解决方案我愿意尝试)来生成漂亮干净的 URL,因此 site.com/index.php?page=2 可以改为 site.com/tools

据我所知,每次添加新页面时我都需要更改我的 .htaccess 文件,这就是我遇到问题的地方,我的 PHP 一直告诉我无法更新它,因为它没有权限。快速的 chmod 显示即使有 777 权限它也不能这样做,我是不是错过了什么?

我的 mod_rewrite 指令来源目前是 this page here以防它很重要/有用。

最佳答案

一种方法是将所有内容重写为处理脚本

RewriteEngine on
RewriteBase /

# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s

# pass the rest of the request into index.php to handle
RewriteRule ^(.*)$ /index.php/$1 [L]

所以如果你有一个请求到http://yourserver/foo/bar/

您实际得到的是对 http://yourserver/index.php/foo/bar 的请求- 你可以让 index.php 决定如何处理/foo/bar (使用 $_SERVER['PATH_INFO'] - tom )

您只需要在第一次修改.htaccess。以后所有对不存在文件的请求都可以在 PHP 中处理。

您可能还会找到 the docs for mod_rewrite有用 - 但要保持简单或准备失去大量 sleep 和头发来追踪模糊的错误。

关于php - mod_rewrite、php 和 .htaccess 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/265898/

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