Search code examples
phpsymfonymodel-view-controllerroutessymfony4

My Symfony routes are throwing a 404?


I'm trying to get back into development, trying to set up Symfony on a shared hosting server with GoDaddy.

I am going through the tutorial with Symfony 4 (Here: http://symfony.com/doc/current/page_creation.html) but the URL.co.uk/lucky/number is throwing a 404.

I set the exact same URL route to just / (instead of lucky/number) and it works fine.

This problem occurs with both routes.yaml, and annotations.

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class ArticleController

{
    /**
     * @Route("/")
     */
    public function homepage()
    {
        return new Response('OMG! My new first page already! ');
    }

    /**
     * @Route("/news")
     */
    public function news()
    {
        return new Response('This must be news?');
    }

}

For example, the above works fine with /, but with /news I have a 404 error.

Could this be a problem with .htaccess? I don't have one in my root.

It turns out that, with the above code, URL.co.uk/index.php/news works just fine. obviously I don't want this, but I hope it helps get to the bottom of it...


Solution

  • symfony 4 does no longer have a .htaccess file in the public folder. So there is no rewrite rules. You have to configure your vhost as explained in the documentation: https://symfony.com/doc/master/setup/web_server_configuration.html

    If you don't have access to the vhost file, you still can configure it in a .htaccess file

    there is an extract of my vhost file:

    DocumentRoot /var/www/html/public/
    
        <Directory /var/www/html/public/>
            Options FollowSymlinks
            AllowOverride None
            Order Allow,Deny
            Allow from All
    
            <IfModule mod_rewrite.c>
                Options -MultiViews
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteRule ^(.*)$ index.php [QSA,L]
                RewriteCond %{HTTP:Authorization} ^(.*)
                RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
            </IfModule>
    
        </Directory>