8

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

sorak
  • 2,607
  • 2
  • 16
  • 24
Marshiewooooooo
  • 179
  • 1
  • 2
  • 12
  • When you hit `/news`, you get a 404 from symfony or from apache/your web server? If you are not sure, paste the error message here. – Federkun Mar 11 '18 at 20:52
  • Not Found The requested URL /news was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. – Marshiewooooooo Mar 11 '18 at 20:58
  • 3
    That's apache's error message. So, it's not a misconfiguration of symfony: the problem is that when you hit `/news` you are not calling symfony at all. What you could do is to install the https://github.com/symfony/apache-pack meta package. It's just a quick way to add a .htaccess file ( https://github.com/symfony/recipes-contrib/blob/master/symfony/apache-pack/1.0/public/.htaccess ) inside your web directory. `composer require symfony/apache-pack` – Federkun Mar 11 '18 at 21:03
  • Okay so installing apache-pack didn't solve it, and it didn't add .htaccess anywhere that I could see - However, manually adding the .htaccess from the link that you provided has fixed the issue!! :) Do you want to answer the question officially? Thank you so much =D – Marshiewooooooo Mar 11 '18 at 21:17
  • apache-pack solved the problem for me, you need to redirect all routes to index.php which takes it from there (into symfony kernel). to do this, you need to setup a .htaccess file in your public folder. this is automatically done with [ **composer require symfony/apache-pack** ] – clockw0rk Aug 03 '20 at 14:26

5 Answers5

14

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>
MatMouth
  • 858
  • 2
  • 7
  • 24
  • 3
    dumping this command here since many ppl. only search for accepted answere: [ **composer require symfony/apache-pack** ] -> will automatically setup everything for you. I hope this helps anybody – clockw0rk Aug 03 '20 at 14:28
5

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.

Marshiewooooooo
  • 179
  • 1
  • 2
  • 12
3

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
Community
  • 1
  • 1
Juergen Schulze
  • 1,515
  • 21
  • 29
  • If you get the following error using above directive: `Invalid command 'AllowOverrides'`, you'll probably have a newer Apache version with a slightly altered syntax, see the syntax in MatMouth's answer. – Graftak Jul 14 '22 at 11:07
1

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

Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23
-1

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

  • 2
    bro please throw a little details. – ahmednawazbutt Jul 21 '22 at 07:15
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32289541) – Simas Joneliunas Jul 24 '22 at 02:30