Apache2 不使用路由 PHP [英] Apache2 not working with routes PHP

查看:44
本文介绍了Apache2 不使用路由 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP 7.0 开发应用程序并使用 MVC 实现路由.我的根文件夹 ('/') 是 'public' 目录.当我访问地址localhost"时,我被重定向到 index.php 并提供可用的路由.但是,当我尝试使用另一个 url 访问另一个路由时,例如localhost/contact",服务器找不到该条目并给出以下消息:

I am developing an app with PHP 7.0 and implementing routes with MVC. My root folder ('/') is the 'public' directory. When I access the address 'localhost' I am redirected to index.php with have the routes available. But when I try another url to access another route, like 'localhost/contact' the server doesn't find the entry and give this message:

Not Found

The requested URL /contact was not found on this server. 

我很确定问题出在我的服务器配置上(linux mint 18 上的 apache2),因为我朋友的 PC 工作正常.我也在公共目录中使用了 .htaccess 文件:

I am pretty sure that the problem in on my server config (apache2 on linux mint 18), because my friend's PC works normal. I'm using a .htaccess file too inside the public directory:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

在我看来,服务器没有运行 index.php 并尝试访问路径.我怎么能强制执行 index.php 来查看是否有到 url 的路由通知?

It seems to me that the server does not run index.php and tries to access the path. How could I ever force the execution of index.php to see if there is a route to the url informed?

这里是我的 apache2 配置文件.

here follows my apache2 config files.

http://pastebin.com/2mgSjWWV

http://pastebin.com/yD4RpfK8

我还是新手,对服务器配置了解甚少.请问有人可以给我一盏灯吗?谢谢!

I'm still newbee and I understand very little in server configuration. Please someone can give me a light? Thanks!

推荐答案

这个:

RewriteRule ^.*$ - [NC,L]

它匹配所有内容,并且因为它被标记为 [L],所以不会评估其他 RewriteRules,所以所有重写都在这里停止.这意味着对 example.com/foo 的请求将匹配此规则,重写停止,并且将在文件系统中搜索文字 foo 文件 - 这不会存在.

It matches EVERYTHING, and since it's tagged [L], no other RewriteRules will be evaluated, so all rewriting stops here. That means a request for example.com/foo will match this rule, rewriting stops, and a literal foo file will be searched for in the file system - which doesn't exist.

然后,即使没有这条规则,下一行也行

And then, even if this one rule wasn't there, this next line

RewriteRule ^.*$ index.php [NC,L]

也不行.任何 url 都会匹配它,但随后会删除相关数据,因此对 example.com/foo 的请求将与对 example.com/index.php 的请求相同>.不会传入任何查询参数.

would also not work. ANY url would match it, but then strip off the relevant data, so a request for example.com/foo would be identical to a request for example.com/index.php. no query parameters would be passed in.

你的逻辑应该更像是:

RewriteRule ^(.*)$ index.php?args=$1  [QSA,L]

可以进行这些类型的翻译:

which would do these sorts of translations:

example.com/foo      -> example.com/index.php?args=foo
example.com/bar?baz  -> example.com/index.php?args=bar&baz

这篇关于Apache2 不使用路由 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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