Marshiewooooooo
Marshiewooooooo

Reputation: 329

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...

Upvotes: 11

Views: 23936

Answers (5)

instead of URL.co.uk/lucky/number try putting URL.co.uk/index.php/lucky/number

Upvotes: -3

Juergen Schulze
Juergen Schulze

Reputation: 1642

You (like I) are missing .htaccess for symfony

Solved with this one liner:

composer require symfony/apache-pack

Make sure the .htaccess file will be processed by apache2:

<Directory /var/www/project/public>
    AllowOverrides All
</Directory>

mod_rewrite needs to be enabled:

a2enmod rewrite
service apache2 restart

Upvotes: 5

Marshiewooooooo
Marshiewooooooo

Reputation: 329

Install .htaccess into the root directory with same code as in this link:

https://github.com/symfony/recipes-contrib/blob/master/symfony/apache-pack/1.0/public/.htaccess

Thank you.

Upvotes: 6

MatMouth
MatMouth

Reputation: 943

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>

Upvotes: 18

Imanali Mamadiev
Imanali Mamadiev

Reputation: 2654

First install composer require annotations :

Then you are using other class Route:

Change

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

to:

use Symfony\Component\Routing\Annotation\Route;

Finally check your routes:

php bin/console debug:router

Upvotes: 1

Related Questions