問題描述
我在共享主機上有一個 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 文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!