apache PHP 是否允许修改当前文件夹中的 .htaccess 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/566950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is PHP allowed to modify .htaccess file in current folder?
提问by AlexA
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.
我有一个位于共享主机上的 PHP 网络应用程序。我的目标是在 PHP 页面运行时从 PHP 代码修改 .htaccess 文件。我需要 .htaccess 将几行 mod_rewrite 插入其中。
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):
问题是,在 Windows+Apache 上,我可以动态修改 .htaccess 文件,但是当我尝试以任何方式(复制或 fopen)访问此文件时,Linux 上的相同代码会报告问题:
"failed to open stream: Permission denied"
"failed to open stream: Permission denied"
I have given .htaccess file 777 permissions - still no result. WHat prevents me from doing this? How can I develop a workaround?
我给了 .htaccess 文件 777 权限 - 仍然没有结果。是什么阻止我这样做?我该如何制定解决方法?
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.
PS 我最初的目标是能够将新的 RewriteRule 添加到 .htaccess 中,将新添加的 category_id 映射到新的 category_name。
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.
如果它不是共享主机,我会使用类似 RewriteMap(在主 Apache 配置中)的东西,并且能够访问映射文件。
This is the first real limitation I've been unable to tackle with PHP+Apache, but I hope it's circuventable too.
这是我无法用 PHP+Apache 解决的第一个真正的限制,但我希望它也可以避免。
回答by Chad Birch
This seems like an overly-complex solution to just having a general "load category" page that takes the category name from the URL and loads the corresponding ID.
这似乎是一个过于复杂的解决方案,因为只有一个通用的“加载类别”页面,从 URL 获取类别名称并加载相应的 ID。
For example, if the URL is:
例如,如果 URL 是:
http://yoursite.com/category/programming
I would remap that to something like:
我会将其重新映射为:
http://yoursite.com/category.php?name=programming
回答by Jasper
I want to suggest something else that also works. Instead of writing a rule for every 'special' url, why not use one for all?
我想建议其他一些也有效的东西。与其为每个“特殊”网址编写规则,为什么不为所有网址使用一个规则?
I found it a whole lot easier to use what wordpress uses: every url is redirected to the index.
我发现使用 wordpress 使用的东西要容易得多:每个 url 都被重定向到索引。
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.
您所要做的就是设置索引文件,读取加载的目录(可能使用 $_SERVER['URI_REQUEST']),然后处理它。
add to .htaccess this:
添加到 .htaccess 中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
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!
多亏了那个大块头,您才能拥有一个不受限制的系统。如果您想重命名您的类别 url,或添加另一个特殊情况,它已经准备好了!
回答by Bo Jeanes
While I agree with the above comments, it can definitely be done. PHP apps like WordPress do exactly this based on changes made on the settings page. It should be as simple as writing the file out however the parent directory NEEDS to have permission for the web server user to write to it.
虽然我同意上述评论,但绝对可以做到。PHP 应用程序(如 WordPress)正是根据在设置页面上所做的更改来执行此操作的。它应该像写出文件一样简单,但是父目录需要具有 Web 服务器用户写入它的权限。
If it isn't working for you the trick will be making the parent directory either 777 or 775 and having the group set to whatever group Apache runs under (usually "apache" or "www" or something similar)
如果它对您不起作用,则技巧是将父目录设置为 777 或 775,并将组设置为 Apache 运行的任何组(通常是“apache”或“www”或类似的东西)
Adam (commented on your question) is quite correct though, some other security layer on your server might be preventing you from doing this, and this is probably a good indication that you might be approaching the problem the wrong way.
Adam(对您的问题发表评论)是非常正确的,您服务器上的其他一些安全层可能会阻止您这样做,这可能是一个很好的迹象,表明您可能以错误的方式解决问题。
回答by greyfade
You only need a small set of rewrite rules. To do what Chad suggests:
您只需要一小组重写规则。按照乍得的建议去做:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/category/.*$ category.php [QSA]
Thus, anytime someone navigates to /category/category_id, the request will be directed to category.php, which will be handed the /category/URI in $_SERVER['REQUEST_URI'], from which you can easily get the category ID, and you don't need to bother with editing the .htaccess file every time you add a category.
因此,无论何时有人导航到/category/category_id,请求都将被定向到category.php,该/category/URI将传递给URI $_SERVER['REQUEST_URI'],您可以从中轻松获取类别 ID,并且每次添加时您都无需费心编辑 .htaccess 文件一个类别。
回答by chaos
I agree with Chad Birch. In case you can't be dissuaded, though, in your situation I would first be looking for parent directories with locked-down permissions.
我同意乍得伯奇的观点。但是,如果您不能被劝阻,在您的情况下,我会首先寻找具有锁定权限的父目录。
FYI, one of the reasons that rewriting the .htaccess is a bad idea is that any requests that come in whilethe .htaccess is being rewritten will not have any of your redirects applied.
仅供参考,原因是重写的.htaccess是一个坏主意之一是,该进来的任何请求,而应用的.htaccess被改写不会有任何您的重定向。

