duanliang1999 2016-10-06 20:42
浏览 40
已采纳

Apache2不使用PHP路由

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. 

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]

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?

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!

  • 写回答

2条回答 默认 最新

  • dongmo9996 2016-10-06 20:57
    关注

    This:

    RewriteRule ^.*$ - [NC,L]
    

    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]
    

    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.

    Your logic should be more like:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号