3

I get 404 Not Found for all request that are not /. Hitting smfony.test in the browsers opens the web page without a problem. But anything other than symfony.test will result in a 404 Not Found. I looked up several stack overflow solutions (Issue 1, Issue 2), but I was not able to locate the exact problem. Seems like it has to be a misconfiguration from my side, because when I run debug:router I get all URLs:

jernej@blackbook:/var/www/html/symfart$ php bin/console debug:router
 ------------------- -------- -------- ------ -------------------------- 
  Name                Method   Scheme   Host   Path                      
 ------------------- -------- -------- ------ -------------------------- 
  _preview_error      ANY      ANY      ANY    /_error/{code}.{_format}  
  app_article_index   GET      ANY      ANY    /                         
  app_article_save    GET      ANY      ANY    /article/save             
  app_article_hello   GET      ANY      ANY    /hello                    
 ------------------- -------- -------- ------ -------------------------- 

Inside my project (/dir path /var/www/project/public) I also have the .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>

Since I do not have a lot of experience with Apache server, I rather point out I also did the following change in the Apache configuration (/etc/apache2/sites-enabled/000-default.conf) to add project to url:

<VirtualHost *:80>
    ServerName symfony.test
    DocumentRoot /var/www/html/project/public
</VirtualHost>

I also added the URL to hosts file in linux. And here is the Controller class:

<?php

    namespace App\Controller;

    use App\Entity\Article;

    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

    class ArticleController extends AbstractController {

        /**
         * @Route("/", methods={"GET"})
         */
        public function index() {
            $articles = array();

            return $this->render('articles/index.html.twig', array('articles' => $articles));
        }

        /**
         * @Route("/article/save", methods={"GET"})
         */
        public function save() {
            $entityManager = $this->getDoctrine()->getManager();

            $article = new Article();
            $article->setTitle('Article One');
            $article->setBody('This is the body for article one');

            $entityManager->persist($article);
            $entityManager->flush();

            return new Response('Saved an article with the id of ' + $article->getId());
        }

        /**
         * @Route("/hello", methods={"GET"})
         */
        public function hello() {
            $articles = array();

            return $this->render('articles/index.html.twig', array('articles' => $articles));
        }
    }

?>
Jernej K
  • 1,602
  • 2
  • 25
  • 38

2 Answers2

6

In this case, you need to allow apache2 to execute .htaccess files from the web root directory...

<VirtualHost *:80>
    ServerName symfony.test
    DocumentRoot /var/www/html/project/public

    <Directory /var/www/html/project/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>
    
</VirtualHost>

And then, you need to enable mod_rewrite. You can find this mod in your .htaccess and it's responsible for URL modifications...

sudo a2enmod rewrite
sudo systemctl restart apache2
xayer
  • 413
  • 4
  • 11
0

Does your homepage work? If it doesn't try to change the default hosting path directory for your website from your server.

  • It works for the root page simfony.test, I get the web page back. But any other URL returns a 404. – Jernej K Mar 23 '20 at 19:36
  • 1
    Try index.php/hello if that works try to install the symfony apache pack – Frank B Mar 24 '20 at 00:17
  • If I do that, then it works. I am able to see the hello message. Any idea how to resolve this? – Jernej K Mar 24 '20 at 11:59
  • I already tried to install the apache pack, if you take a look at the solution 2 link. Running `composer require symfony/apache-pack` will not resolve the issue. – Jernej K Mar 24 '20 at 12:22