htaccess Symfony2将所有* .php页面(但app.php)重定向到app.php

I would like to modify my htaccess to :

  1. Redirect all *.php calls to app.php (SEO call to keep ranking from migration)
  2. Avoid redirection loop excluding app.php for rewrite rule above.

I ended up with this :

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !app\.php$
    RewriteRule ^(.*)\.php?$ /app.php/ [R=301,L]

   [...]

</IfModule>

But this produces a 404 on test url like http://example.com/sqdlkqjsdlsqk.php

What's wrong in here?

Regards,

You should be using this rule without trailing slash after app.php:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/app\.php$ [NC]
RewriteRule ^(.+)\.php$ /app.php [R=301,L,NC,NE]

Make sure to clear browser cache before testing this change.

Check this out. Make sure to clear your cache.

DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>