mod_rewrite的,PHP和.htaccess文件 [英] mod_rewrite, php and the .htaccess file
问题描述
我编码小CMS得到更好地了解他们的工作和学习PHP的一些新的东西。然而,我所遇到的一个问题。
I'm coding a small CMS to get a better understanding of how they work and to learn some new things about PHP. I have however come across a problem.
我要使用mod_rewrite(尽管如果有人有一个更好的解决方案我为尝试它)来产生漂亮干净的URL,因此site.com/index.php?page=2可以改为将site.com/tools
I want to use mod_rewrite (though if someone has a better solution I'm up for trying it) to produce nice clean URLs, so site.com/index.php?page=2 can instead be site.com/tools
根据我的理解,我需要每一次我添加了新的一页,以改变我的.htaccess文件,这是我取得一个问题,我的PHP一直告诉我,因为它并没有权限,我不能更新。文件模式的快速位显示,即使有777权限也不能做到这一点,我失去了一些东西?
By my understanding I need to alter my .htaccess file each time I add a new page and this is where I strike a problem, my PHP keeps telling me that I can't update it because it hasn't the permissions. A quick bit of chmod reveals that even with 777 permissions it can't do it, am I missing something?
我的源mod_rewrite的说明,目前此页这里柜面是非常重要的/有用的。
My source for mod_rewrite instructions is currently this page here incase it is important/useful.
推荐答案
一种方法是重写一切到处理脚本
One approach is to rewrite everything to a handling script
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 /富/酒吧/
你真正得到的是一个请求 HTTP://yourserver/index.php/foo/bar - 和你可以离开的index.php,以决定如何处理/富/条(使用$ _ SERVER ['PATH_INFO'] - 汤姆)
what you actually get is a request to http://yourserver/index.php/foo/bar - and you can leave index.php to decide what to do with /foo/bar (using $_SERVER['PATH_INFO'] -tom)
您只需要修改.htaccess中的第一次。对于不存在的文件,今后所有申请就可以在PHP处理。
You only need to modify .htaccess the first time. All future requests for inexistent files can then be handled in PHP.
您可能还会发现的文档mod_rewrite的有用的 - 但保持它的简单或prepare失去大量的睡眠和头发的跟踪隐蔽的错误。
You might also find the docs for mod_rewrite useful - but keep it simple or prepare to lose a lot of sleep and hair tracking down obscure errors.
这篇关于mod_rewrite的,PHP和.htaccess文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!