在Laravel 4中重定向301

时间:2015-02-25 10:24:36

标签: .htaccess laravel

我在Laravel 4中构建了一个新网站,它将取代使用基本html页面构建的旧网站。当我上线时,我需要将旧页面重定向到新结构。

即。 website.co.uk/ourpolicy.html到website.co.uk/ourpolicy

我通常会在.htaccess文件中为非Laravel项目执行此操作。

我现在的.htaccess: -

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我会将这样的重写添加到htaccess中,还是在Laravel中有更好的方法?

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    redirect 301 /ourpolicy.html http://www.website.co.uk/ourpolicy

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

1 个答案:

答案 0 :(得分:1)

您可以制定通用规则以从网址中删除.html

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteRule ^business-london\.html$ /london/business [L,NC,R=301]

    RewriteRule ^(.+?)\.html$ /$1 [L,NC,R=301]

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>