加mod-rewrite到现有的PHP网站 [英] Adding mod-rewrite to an existing PHP website

查看:269
本文介绍了加mod-rewrite到现有的PHP网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新的PHP应用程序,它目前不使用URL重写。这样做的目的是要隐藏的文件扩展名。是网站的一般结构如下

I am updating a php app, which currently does not use url rewriting. The aim is to hide file extensions. The general structure of the website is as follows

root/
  index.php
  login.php
  page1.php
  page2.php
  page3.php
  page4.php
  page5.php
  page6.php
  page7.php
  page8.php
subfolder/
  index.php
  validpage.php
images/
css/

在上述结构的子文件夹是在于它是包含其他文件的的PHP files.All唯一子是在根文件夹独特

The subfolder folder in the above structure is unique in that it is the only subfolder containing php files.All of the other files are in the root folder.

我经历过这些问题<一href="http://stackoverflow.com/questions/788188/mod-rewrite-and-php">http://stackoverflow.com/questions/788188/mod-rewrite-and-php和<一href="http://stackoverflow.com/questions/1868154/mod-rewrite-or-php-router">http://stackoverflow.com/questions/1868154/mod-rewrite-or-php-router和<一href="http://stackoverflow.com/questions/265898/mod-rewrite-php-and-the-htaccess-file">http://stackoverflow.com/questions/265898/mod-rewrite-php-and-the-htaccess-file

I have been through these questions http://stackoverflow.com/questions/788188/mod-rewrite-and-php and http://stackoverflow.com/questions/1868154/mod-rewrite-or-php-router and http://stackoverflow.com/questions/265898/mod-rewrite-php-and-the-htaccess-file

更新 htaccess的。感谢 @thomasmalt

Updated htaccess. thanks to @thomasmalt

RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l 
RewriteRule ^.*$ - [NC,L]


# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# add .php to the url     
RewriteRule ^(.*)$ $1.php

现在的问题是,我收到了500,而不是404错误,当我试图访问一个不存在的页面像本地主机/ inex.php。在 Apache的错误日志给我下面的错误。 (我访问本地主机/指数,加载的index.php由我装本地主机/ INEX这给了我一个500错误的内容,方法同上。)

Problem now is I get a 500 instead of a 404 error, when I try to access a non existent page like localhost/inex.php. The Apache error log gives me the following errors. (I accessed localhost/index which loaded the contents of index.php , followed by which I loaded localhost/inex which gave me a 500 error.)

[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

更新:找到了答案,我的问题<一href="http://stackoverflow.com/questions/1745680/htaccess-rewrite-causes-500-error-instead-of-404/1750696#1750696">here.问题是解决了。

Update: found the answer to my question here. The question is resolved.

推荐答案

我不完全确定你想要达到的目的,我怀疑,没有不敬打算,你需要了解双方的mod_rewrite,和路在网络服务器和PHP的解析和执行的文件。我不知道如何在不投篮命中率超过或低于你的理解水平来回答,但我会尽力。

I'm not totally sure what you're trying to achieve, and i suspect, no disrespect intended, that you need to understand both mod_rewrite, and the way the webserver and php parses and executes files. I'm not sure how to answer without shooting over or under your level of understanding, but I'll try.

我可以告诉你,你在做什么目前的.htaccess是这样做的:(伪code)

I can tell you what you're current .htaccess is doing: (pseudocode)

if REQUEST_FILENAME does not exist then
    rewrite request to /index.php/<the request>

由于index.php的是执行,一切都摆在$ 1忽略了你的PHP脚本。 这就是为什么index.php文件被执行不管你做什么。

Since index.php is your php script that is executed and everything put in $1 is ignored. That's why index.php is executed whatever you do.

有什么你可以尝试把你的.htaccess是这样的:

What you could try to put in your .htaccess is this:

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^(.*)$ $1.php

这应请求并尝试添加的.php到它的结束。我没有测试过不幸,但你应该试一试。结果应该是这样的:

That should take the request and try to append .php to the end of it. I haven't tested it unfortunately, but you should give it a try. The result should be something like this:

if request is "/news/article" 
    translate it to "/news/article.php"

和你应该一天两次读取mod_rewrite的文档了一个月,买了O'Reilly的书关于普通防爆pressions。

And you should read the mod_rewrite documentation twice a day for a month, and buy the O'Reilly book about Regular Expressions.

如果您拥有管理员权限的开发服务器,你应该看看

And if you have administrator access to the development server you should look into

RewriteLogLevel
RewriteLog

这篇关于加mod-rewrite到现有的PHP网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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