Apache2无法使用路由PHP [英] Apache2 not working with routes PHP
问题描述
我正在用PHP 7.0开发一个应用程序,并用MVC实现路由。我的根文件夹('/')是'public'目录。当我访问地址本地主机时,我将重定向到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)上,因为我朋友的电脑正常工作。我也在公共目录中也使用.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并尝试访问路径。
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.
我仍然是新手,对服务器配置了解得很少。请有人可以给我点灯吗?谢谢!
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屋!