简体   繁体   English

mod_rewrite,php和.htaccess文件

[英]mod_rewrite, php and the .htaccess file

I'm coding a small CMS to get a better understanding of how they work and to learn some new things about PHP. 我正在编写一个小型CMS来更好地理解它们的工作方式,并学习一些关于PHP的新东西。 I have however come across a problem. 然而,我遇到了一个问题。

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 我想使用mod_rewrite(虽然如果有人有更好的解决方案我尝试它)来生成漂亮干净的URL,所以site.com/index.php?page=2可以改为site.com/tools

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. 根据我的理解,我需要在每次添加新页面时更改我的.htaccess文件,这是我遇到问题的地方,我的PHP一直告诉我,我无法更新它,因为它没有权限。 A quick bit of chmod reveals that even with 777 permissions it can't do it, am I missing something? 快速的chmod显示,即使拥有777权限也无法做到,我错过了什么?

My source for mod_rewrite instructions is currently this page here incase it is important/useful. 我的mod_rewrite指令源目前是此页面,因为它很重要/有用。

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]

so if you have a request to http://yourserver/foo/bar/ 所以,如果您有http:// yourserver / foo / bar /的请求

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 ) 你真正得到的是对http://yourserver/index.php/foo/bar的请求 - 你可以留下index.php来决定如何处理/ foo / bar(使用$ _SERVER ['PATH_INFO'] - tom

You only need to modify .htaccess the first time. 你只需要第一次修改.htaccess。 All future requests for inexistent files can then be handled in PHP. 然后可以在PHP中处理将来对不存在文件的所有请求。

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的文档很有用 - 但要保持简单或准备丢失大量的睡眠和头发跟踪晦涩的错误。

Your PHP should for very obvious reasons not be able to modify .htaccess . 您的PHP应该由于非常明显的原因而无法修改.htaccess Even if you get that to work, I'm not sure if it is wise. 即使你让它工作,我也不确定它是否明智。

How about using a more abstract setup in regard to mod_rewrite rules? 如何在mod_rewrite规则中使用更抽象的设置? Define your general URL pattern, as you would like to use it. 根据您的意愿定义您的常规URL模式。 For example: 例如:

/object/action/id

Then write a set of rules that reroute HTTP requests to a PHP page, which in turn makes the decision what page is to run (say, by including the relevant PHP script). 然后编写一组规则,将HTTP请求重新路由到PHP页面,从而决定运行哪个页面(例如,通过包含相关的PHP脚本)。

RewriteRule ^/(\w+)/?(\w+)?/?(\d+)?$ /index.php?object=$1&action=$2&id=$3 [nocase]

This way you would not have to update .htaccess very often an still be flexible. 这样你就不必经常更新.htaccess仍然是灵活的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM