大网数据广告活动
网站运营与故障问答

FrontController dosen´t work

· 52CE 我爱测

i have a example FrontController, its really simple. index.php: if (isset($_GET['page'])) { $requested_page = $_GET['page']; } else { $requested_page = 'home'; } switch($requested_page) { case "blog": include(__DIR__."/blog.php")…

问题原文

原帖链接 · Denis Jurkovsek · CC BY-SA 4.0

i have a example FrontController, its really simple.

index.php:

if (isset($_GET['page'])) {
  $requested_page = $_GET['page'];
} else {
  $requested_page = 'home';
}

switch($requested_page) {
   case "blog":
      include(__DIR__."/blog.php");
      break;
   case "home":
      include(__DIR__."/home.php");
      break;
   default:
      include(__DIR__."/help.php");
}

.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php [NC,L,QSA]

But this dosen´t work, i have tried to give the index.php the parameter "page", but this also dont work.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

i have tried to give the index.php the parameter "page"

Although you've not included this in the example you posted - so what you have posted will obviously not work because of this. If your front-controller is dependent on a page URL parameter then you need to pass this in the rewritten URL. For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /index.php?page=$1 [QSA,L]

I assume you've already included the necessary RewriteEngine On directive?

(.+) - You don't need to rewrite the empty URL-path, since mod_dir will issue an internal subrequest for index.php by default.

The NC flag is not required here, because your regex is not case dependent.

([^?]*) - The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. So checking for anything other that ? in the regex is generally pointless, unless you have a specific requirement?


UPDATE#1: ...it still dont work ...maybe because of the path? It is in "localhost/controller"

I assume from this that index.php is in the /controller subdirectory, ie. /controller/index.php and your .htaccess file is in the document root, and you are requesting URLs of the form example.com/blog. In which case your RewriteRule directive should be of the form:

RewriteRule (.+) /controller/index.php?page=$1 [QSA,L]

UPDATE#2: ...my .htaccess is in the /controller directory ... The format is localhost/controller/filename.

In that case the above should still work, although you will need to make sure that MultiViews is disabled when accessing /controller/blog, .../home or .../help since these map directly to files of the same name but with a .php file extension.

MultiViews should not be enabled by default, however, some shared hosts do enable it for some reason. If MultiViews is enabled then you would expect your include file to be requested directly, essentially bypassing your front-controller. ie. request /blog and /blog.php gets loaded automatically.

If the .htaccess file and index.php are in the same directory then the substitution string can be simplified.

In summary:

# Disable MultiViews
Options -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php?page=$1 [QSA,L]

The pattern .+ specifically matches 1 or more characters, so this naturally excludes requests for the directory itself (ie. /controller/). However, mod_dir will handle this and direct the request to index.php, providing your DirectoryIndex is set correctly.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-04-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。