PHP 是否允许修改当前文件夹中的 .htaccess 文件?

我在共享主机上有一个 PHP 网络应用程序.我的目标是在 PHP 页面运行时从 PHP 代码修改 .htaccess 文件.我需要 .htaccess 将几行 mod_rewrite 插入其中.

I have a PHP web app located on shared hosting. My goal is to modify .htaccess file from PHP code when the PHP page is running. I need that .htaccess to insert a couple of mod_rewrite lines into it.

问题是在Windows+Apache上我可以动态修改.htaccess文件但是当我尝试以任何方式访问此文件时,Linux 上的相同代码会报告问题方式(复制或 fopen):

The problem is that on Windows+Apache I can dynamically modify .htaccess file but the same code on Linux reports a problem when I try to access this file in any way (copy or fopen):

无法打开流:权限被拒绝"

我给了 .htaccess 文件 777 权限 - 仍然没有结果.是什么阻止我这样做?我该如何制定解决方法?

I have given .htaccess file 777 permissions - still no result. WHat prevents me from doing this? How can I develop a workaround?

附言我最初的目标是能够在 .htaccess 中添加一个新的 RewriteRule 来映射一个新添加的 category_id 和新的 category_name.

P.S. My initial goal was to be able to add a new RewriteRule into .htaccess that maps a newly added category_id with new category_name.

如果不是共享主机,我会使用 RewriteMap(在主 Apache 配置中)之类的东西,并且能够访问地图文件.

If it wasn't shared hosting, I would use something like RewriteMap (in main Apache config) and would be able to access the map file.

这是我无法使用 PHP+Apache 解决的第一个真正的限制,但我希望它也可以避免.

This is the first real limitation I've been unable to tackle with PHP+Apache, but I hope it's circuventable too.

推荐答案

我想建议其他一些也有效的方法.与其为每个特殊"网址编写规则,为什么不为所有网址使用一个规则?

I want to suggest something else that also works. Instead of writing a rule for every 'special' url, why not use one for all?

我发现使用 wordpress 使用起来要容易得多:每个 url 都被重定向到索引.

I found it a whole lot easier to use what wordpress uses: every url is redirected to the index.

您所要做的就是设置索引文件,读取加载的目录(可能使用 $_SERVER['URI_REQUEST']),然后处理它.

All you have to do is, set up the index file, read in the directory that was loaded (perhaps using $_SERVER['URI_REQUEST']), and deal with it.

添加到.htaccess:

add to .htaccess this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

多亏了那个笨蛋,你才有了一个可以随意使用的系统.如果您想重命名您的类别 url,或添加另一个特殊情况,它已经准备好了!

Thanks to that chunck you have a system somewhat unlimited at your disposal. If you ever feel like renaming you categrory url, or add another special case, it's already ready!

相关文章