6

Consider the following URL : http://www.myurl.fr/accueil.

It won't work. However http://www.myrurl.fr/app.php/accueil does work.

I got rid of the .htaccess file because I want to use the vhost file and rely on Apache routing. My vhost file is as follow:

<VirtualHost my.ip.address>
ServerName myurl.fr
ServerAlias www.myurl.fr
DocumentRoot /var/www/mysite/web
DirectoryIndex app.php
<Directory "/var/www/mysite/web">
  AllowOverride All
  Allow from All
</Directory>

<IfModule mod_rewrite.c>
 RewriteEngine On  
 RewriteCond %{ENV:REDIRECT_STATUS} ^$  
 RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
 RewriteRule .? - [L]
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteRule ^(.*)$ app.php [QSA,L]
 RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
 RewriteRule ^(.*) - [E=BASE:%1]    
 RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
</VirtualHost>

I've cleared Apache cache and restarted it many times. The urlrewrite mod is enabled. I just don't know what else to check. Any idea what I'm missing ?

EDIT It could be that both URL won't work at a given time since I'm working on it. My issue is still valid.

2
  • You can change AllowOverride All to AllowOverride None. Unlikely to solve your issue tho. Commented Aug 16, 2013 at 20:22
  • yes, that's not going to solve my issue :) Commented Aug 16, 2013 at 20:23

6 Answers 6

5

Compared your rules with symfony-standard .htaccess, I saw that you missed file checking before 'pass everything rule'. Try to

<IfModule mod_rewrite.c>
    RewriteEngine On  

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
    # 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}/app.php [L]
</IfModule>
Sign up to request clarification or add additional context in comments.

3 Comments

that works indeed ! however my css, images and scripts are no longer found. How do I solve that ? I guess I should have a rule to ignore rewriting of these types of file ?
ok I've added three rules to ignore css, img and js folders so they don't get rewritten. I've edited your answer with these 3 rules so I can mark it as answer. Thanks a lot.
@Sam where are the ignore rules for css,img and js folders?
4

The Symfony docs offer this straight forward solution to this problem.

'<VirtualHost *:80>
    ServerName domain.tld
    ServerAlias www.domain.tld

    DocumentRoot /var/www/project/web
    <Directory /var/www/project/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All

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

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    # optionally disable the RewriteEngine for the asset directories
    # which will allow apache to simply reply with a 404 when files are
    # not found instead of passing the request into the full symfony stack
    <Directory /var/www/project/web/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>
    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>'

One thing I would like to add to this discussion is that none of these excellent suggestions will bear fruit without the following consideration.

If you are working with Apache you must be sure to enable mod_rewrite!

`sudo a2enmod rewrite`

So if you're beating your head against the wall, wondering why nothing is working, try this. It just might save your sanity and your project! Happy coding!

3 Comments

Thank you, I found that apache has not enabled mod_rewrite by default, when I enabled it, my config worked perfectly
Glad I was able to help at least one person!
@icarlosmendez Thank you.. your comment "If you are working with Apache you must be sure to enable mod_rewrite! sudo a2enmod rewrite" Is helpful me
2

change the apache httpd.conf virtual host code to this

ServerName my-site.fr
<VirtualHost your.ip.address:80>
DocumentRoot /var/www/mysite/web
<Directory "/var/www/mysite/web">
DirectoryIndex app.php
Options -Indexes FollowSymLinks SymLinksifOwnerMatch
AllowOverride All
Allow from All
</Directory>
</VirtualHost>

2 Comments

This syntax gave me an error with apache. Better use the developed syntax with one instruction by line. Like it's on symfony's doc symfony.com/doc/current/setup/…
yes, my answer is a bit outdated it's 5 years old and now you can have un official apache configuration we did not have that back then
2

The Key solution here is to make sure that mod_rewrite is enabled by typing

a2enmod rewrite

If you have apache 2.4, then check your config file that have instead of Order allow.deny

Require all granted

Here is a sample config inside directory directive

AllowOverride all
Require all granted

Comments

1

@icarlosmendez Your suggestion of "sudo a2enmod rewrite" really saves my day!

This is what I did, hope some people would find it helpful

  1. run "sudo a2enmod rewrite" first
  2. copy the code from symfony, place it under "/etc/apache2/sites-available"
  3. just a note to people who got 500 errors, check that var under project folder got 777.

2 Comments

> copy the code from symfony, place it under "/etc/apache2/sites-available" What? This should not work, sites-available is where apache vhost configuration is kept.
That's what I mean. Copy the second section of the code from Apache with mod_php/PHP-CGI section.
0

Add this code to your apache conf /etc/apache2/sites-available/000-default.conf

and make sure that mod_rewrite is enabled by typing

a2enmod rewrite

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/web/
    DocumentRoot /var/www/html/web
    <Directory /var/www/html/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>
</VirtualHost>

Comments

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.