10

I have been following Symfony 4 documentation as to how to configure a web server.

https://symfony.com/doc/current/setup/web_server_configuration.html

My .conf file in my apache 2.4 configuration is exactly as described in their documentation. I’m copying part of it here :

<Directory /var/www/project/public>
    AllowOverride None
    Require all granted
    Allow from All

    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
</Directory>

Going to my platform, everything works fine but as soon as I’m going to any page, it shows a 404 error. If I add index.php in the url it works fine. Could you help me figure out what is going on?

For example: When I’m on http://myplatform.com/

I can save an email address on this page. If I go to http://myplatform.com/index.php/saveEmail it works, but from my page, my links redirects me to http://myplatform.com/saveEmail and it doesn’t work.

If i go to http://myplatform.com/index.php then everything works fine.

How to I remove index.php from the url ?

Thanks!

Miles M.
  • 4,089
  • 12
  • 62
  • 108
  • 1
    Do you have a .htaccess in your **public** directory ? – fgamess May 31 '18 at 05:52
  • I’m using a .conf file in apache sites-enabled configuration folder – Miles M. May 31 '18 at 16:54
  • But yes it has been generated by apache-pack from composer in my public repo. I’ve copied everything it contains inside my .conf file in the apache configuration “site-enabled” folder. Is that the right thing to do? – Miles M. May 31 '18 at 18:18

4 Answers4

24

I do not know about Symfony. But it should work with all apache2 and php project.


First you need to do two things:

  1. You need to check if php rewrite module is enabled. you can do it with phpinfo(). Write this in a file info.php and open in browser: http://localhost/info.php

    Now search for mod_rewrite. If you see mod_rewrite in this page that means, mod_rewrite is enabled.

  2. If mod_rewrite is not enabled, enable it. I enabled in in ubuntu 18.04 for apache with this command: $ sudo a2enmod rewrite. Then restart apache2 $ systemctl restart apache2

if mod_rewrite is enabled, then other configuration will work.


  1. Then add AllowOverride All in .conf file:

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

  1. Then add .htaccess file in you project root directory.

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Send would-be 404 requests to Craft
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.+) index.php?p=$1 [QSA,L]
    </IfModule>
    

Note: Try adding .htaccess file in /var/www/project/.htaccess or in /var/www/project/public/.htaccess. Not sure what will work.

mahfuz
  • 2,728
  • 20
  • 18
5

This apache configuration doesn't include the redirection to index.php for every request. You have 2 choices:

• On the same page, there is a configuration which include this redirection after this sentence : "Use the following optimized configuration to disable .htaccess support and increase web server performance:"

• If you want to use .htaccess to proccess this redirection, then you could simply execute composer require apache-pack. This command will install a .htaccess in your public directory.

Florian Hermann
  • 750
  • 6
  • 15
  • Thanks. I’ve been using the optimized version you mentioned. The code I pasted above was actually the one from this section. I’ve also installed apache-pack. I’m a bit stuck here. – Miles M. May 31 '18 at 17:43
  • Should I copy and paste all of the .htaccess in my public directory inside of the .conf file in site-enabled? – Miles M. May 31 '18 at 18:17
  • In my opinion there is but one correct way to do it. That's with composer require apache-pack. Why work hard when the problem has already been solved? – Halfstop Nov 16 '18 at 04:39
  • Personally, I use the apache-pack only for the development and in production, I use as recommended the configuration without the .htaccess for optimizations of performances – Florian Hermann Nov 16 '18 at 09:05
0

If you change it this way maybe it can work,

AllowOverride All
Order Allow,Deny
Deniz Aktürk
  • 362
  • 1
  • 9
  • Order Allow,Deny isn’t supported by Apache 2.4. I’m not using .htaccess so I don’t need to do AllowOverride All. I’ve tried but no luck. – Miles M. Jun 01 '18 at 20:51
0

I found the issue. the <Directory /var/www/project/public> was pointing to the wrong directory.

Miles M.
  • 4,089
  • 12
  • 62
  • 108