i have a example FrontController, its really simple.


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

switch($requested_page) {
   case "blog":
   case "home":


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.

  • How were you managing the URL-routing in your other front-controller related question? webmasters.stackexchange.com/questions/121918/…
    – MrWhite
    Apr 2 '19 at 13:03
  • I think this way is better, because i have only 3 lines in the htaccess-file. Or im wrong? Apr 2 '19 at 13:12
  • It's not really about the number of lines in the .htaccess file. The solution I presented in my answer to your other question only had 3 lines as well (and only 1 line you used the FallbackResource directive). It's just a different method (pros and cons of each). My point really is that if the code (even the original code in your question) was "working" then the principles involved here are in fact the same.
    – MrWhite
    Apr 4 '19 at 21:38

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.

  • First thanks for the new information. But it still dont work if i add the "page" parameter. maybe because of the path? It is in "localhost/controller" Apr 2 '19 at 13:13
  • 1
    Where is your .htaccess file and where is index.php? Is /controller a physical directory? This is quite possibly the problem.
    – MrWhite
    Apr 2 '19 at 16:31
  • Thanks for your help, but it still dont work :/ Apr 3 '19 at 8:52
  • And my .htaccess is in the "/controller" directory in "/var/www/html/" Apr 3 '19 at 8:53
  • 1
    Thank you very much, now it works! Apr 5 '19 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.