简体   繁体   English

带有XAMPP和Windows的PHP Front Controller和.htaccess

[英]PHP Front Controller and .htaccess with XAMPP and Windows

I'm trying to get a simple front controller for routing set up. 我正在尝试获得用于路由设置的简单前端控制器。 This is the front-controller.php file I have: 这是我拥有的front-controller.php文件:

<?php

if ($_SERVER['REQUEST_URI'] == '/help') {
    include 'help.php';
}

this is a .htaccess file I have in the folder with my index.php (which is under htdocs/wad) 这是我的index.php文件夹中的.htaccess文件(位于htdocs / wad下)

RewriteEngine On
RewriteRule . /front-controller.php [L]

In httpd.conf I changed all instances of the AllowOverride None line to AllowOverride All and I uncommented the LoadModule rewrite_module modules/mod_rewrite.so line. httpd.conf我将AllowOverride None行的所有实例更改为AllowOverride All并取消了对LoadModule rewrite_module modules/mod_rewrite.so行的注释。

I am getting Error 404 if I try to go to localhost/wad/help and even localhost/wad: http://puu.sh/cE6WT/2e4c645555.png 如果我尝试转到localhost / wad / help甚至localhost / wad,我将收到错误404: http : //puu.sh/cE6WT/2e4c645555.png

The help.php file exists. help.php文件存在。 I want to be able to browse to localhost/wad/help and have it load (in the same page, not redirect to it). 我希望能够浏览到localhost / wad / help并加载它(在同一页面中,而不是重定向到它)。 Am I going about this the wrong way? 我会以错误的方式处理吗?

Thanks 谢谢

I think, there is a much more better way, if you are use your .htaccess like this: 我认为,如果您像这样使用.htaccess,还有一种更好的方法:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /front-controller.php?action=$1 [QSA,L]

And then in your front-controller.php you can use the $_GET["action"] parse it, and route where you want. 然后,在front-controller.php您可以使用$_GET["action"]解析,然后将其路由到所需位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM