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

时间:2016-09-02 08:52:27

标签: php apache .htaccess symfony redirect

我想将我的htaccess修改为:

  1. 将所有* .php调用重定向到app.php(SEO调用以保持迁移排名)
  2. 避免重定向循环,不包括上面重写规则的app.php。
  3. 我最终得到了这个:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_URI} !app\.php$
        RewriteRule ^(.*)\.php?$ /app.php/ [R=301,L]
    
       [...]
    
    </IfModule>
    

    但是这会在http://example.com/sqdlkqjsdlsqk.php

    等测试网址上产生404

    这里有什么问题?

    此致

2 个答案:

答案 0 :(得分:1)

您应该在app.php之后使用此规则而不使用斜杠:

RewriteEngine On

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

确保在测试此更改之前清除浏览器缓存。

答案 1 :(得分:1)

检查一下。确保清除缓存。

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>