mod_rewrite、php 和 .htaccess 文件 [英] mod_rewrite, php and the .htaccess file

查看:28
本文介绍了mod_rewrite、php 和 .htaccess 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型 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 一直告诉我我无法更新它,因为它没有权限.一点点 chmod 显示,即使有 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/foo/bar/

so if you have a request to http://yourserver/foo/bar/

您实际得到的是对 http://yourserver/index.php/foo/bar 的请求 - 你可以让 index.php 决定如何处理/foo/bar(使用 $_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 的文档 很有用 - 但保持简单,或者准备好失去大量睡眠和头发来追踪晦涩的错误.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆