We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Cannot access home Page : mod_rewrite problem?

Hi. I am doing the blog tutorial available on this site. I created a new project with the phalcon CLI. And then tried to access the home page of the project but I get a 404 error. Well all the directories and required files have been generated correctly as far as I can tell and the directory structure looks like this:

/blog
    .htaccess
    /public
        .htaccess
        index.php

The problem seems to be in the first .htaccess file. Indeed, if I enter the URL base/blog then I get a 404 error. but if I go to base/blog/public, then everything is fine and I can see the home page (You are now flying with Phalcon).

The .htaccess looks like this:

      <IfModule mod_rewrite.c>
          RewriteEngine on
          RewriteRule  ^$ public/     [L]
          RewriteRule  (.*) public/$1 [L]
      </IfModule>

This is the standard file generated by the phalcon tool and I can't find what is wrong with that file. My configuration is Windows 7 / EasyPHP / Apache 2.4.7 My project URL is located in a subdir of an aliased URL. I tried to move the project in the Apache webroot directory, just in case the aliased URL would be the problem, but then I get an internal server error due to recursion.

Anyone can help me on that one?

Have you enabled rewrite rules? This is from EasyPHP's FAQ:

In apache configuration file: Uncomment LoadModule rewrite_module modules/mod_rewrite.so

In <Directory "${path}/www"> section replace AllowOverride None by AllowOverride All

Thanx @screenas for your answer. mod_rewrite is indeed enabled and working well.

I did some more tests and finally found that when I move the phalcon project directory under the Apache Main Web Root dir then everything is working well (contrary to what I though initially).

But I have not been able to make my aliased directory work well with the rewriting rules.

I finally resorted to create a virtual host for my original phalcon project directory and it is working the way it should, at last.

But still... I am curious about how I could have made my aliased directory work well at the first place!! If you have a hint, please let me know!

I am not on a windows, so i can't test it, but for example, if you want create a phalcon project 'blog' then you would set up your alias:

  1. create a directory:
    new folder 'blog' in Desktop\www

  2. Create a name for the Alias: blog

  3. Copy the Path..:
    C:Users\Name\Desktop\www\blog

In your index.php

//Setup your base uri
$di->set('url', function(){
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/blog/');
    return $url;
});

blog/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>

blog/public/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

hope it works, and as I said, I can't test.

I have not wandered yet on the side of controller config, but sure will meet the point sooner or later, so your advice will make itself useful when I get there. As for the rewriting stuff, I guess I am done with the issue. Indeed, the virtual server is working well, and the rewriting rules as you put them are just fine. Thanks again @screenas for your answer.

Hi , I have a similar problem , when I work development environment (Ubutu), the proyect run well but when I´m deploying in the produccion enviroment (Centos 6.3) I have the problem with "Mod-Rewrite is not enabled"

Have you Mod-Rewrite enabled? Elso if you are using Virtualhosts your Base URI should be:

$url->setBaseUri('/');

instead of

$url->setBaseUri('/blog/');

(blog is just an example)



60

hi screenas,

i have followed your suggest, but it's not working. My web doesnot show all directory with phalcon. Any Solution ? thank You