5

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));
        }
    }

?>

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

It works for the root page simfony.test, I get the web page back. But any other URL returns a 404.
Try index.php/hello if that works try to install the symfony apache pack
If I do that, then it works. I am able to see the hello message. Any idea how to resolve this?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.