Paul Wright
Paul Wright

Reputation: 455

301 redirects within Laravel 4

I have built a new website in Laravel 4 that will replace an old site built using basic html pages. When I go live, I need to redirect old pages to the new structure.

i.e. website.co.uk/ourpolicy.html to website.co.uk/ourpolicy

I would normally do this in the .htaccess file for a non Laravel project.

My current .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>

Would I just add my rewrite like this to htaccess or is there a better way in 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>

Upvotes: 1

Views: 550

Answers (1)

anubhava
anubhava

Reputation: 785156

You can make a generic rule to strip .html from URLs:

<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>

Upvotes: 1

Related Questions