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

Is PHP allowed to modify .htaccess file in current folder?(PHP 是否允许修改当前文件夹中的 .htaccess 文件?)
本文介绍了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!

这篇关于PHP 是否允许修改当前文件夹中的 .htaccess 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)