Jump to:

329 Posts in 212 Topics by 160 members

Migrating a Site to Silverstripe

SilverStripe Forums » Migrating a Site to Silverstripe » 301 redirect problem, htaccess file and Silverstripe 2.4.5

What you need to know when migrating your existing site to SilverStripe.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3823 Views
  • octopuscreative
    Avatar
    Community Member
    9 Posts

    301 redirect problem, htaccess file and Silverstripe 2.4.5 Link to this post

    I'm currently having problems implementing 301 page redirects on the htaccess file associated with a Silverstripe 2.4.5 install. I've rebuilt a client's old asp based website in Silverstripe and the site has a number of old page references on the search indexes.

    The current htaccess file has the following in place:

    ### SILVERSTRIPE START ###

    <Files *.ss>
       Order deny,allow
       Deny from all
       Allow from 127.0.0.1
    </Files>

    <Files web.config>
       Order deny,allow
       Deny from all
    </Files>

    ErrorDocument 404 /assets/error-404.html
    ErrorDocument 500 /assets/error-500.html

    <IfModule mod_alias.c>
       RedirectMatch 403 /silverstripe-cache(/|$)
    </IfModule>

    <IfModule mod_rewrite.c>
       SetEnv HTTP_MOD_REWRITE On
       RewriteEngine On

       RewriteCond %{REQUEST_URI} ^(.*)$
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
    </IfModule>
    ### SILVERSTRIPE END ###

    I've looked at a few forum posts on htaccess files and 301 redirect problems and tried placing the following RewriteCond on the Silverstripe htaccess file:

       RewriteRule ^news.asp http://www.mydomain.co.uk/news-and-events/latest-news/ [R=301,NC,L]

    Placing the above code at the end of the RewriteRule produced no redirect result on the public page, just a 404 page error ie

    ...   RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
       RewriteRule ^news.asp http://www.mydomain.co.uk/news-and-events/latest-news/ [R=301,NC,L]
    </IfModule>
    ### SILVERSTRIPE END ###

    and placing the code before the Silverstripe copy gave the desired page redirect but the main site navigation links broke? ie

       RewriteRule ^news.asp http://www.mydomain.co.uk/news-and-events/latest-news/ [R=301,NC,L]

    ### SILVERSTRIPE START ###

    <Files *.ss>
       Order deny,allow ...

    Has anyone experienced the same issue with 301 redirects and this type of Silverstripe htaccess file? Any suggestions appreciated!

    Matt

  • octopuscreative
    Avatar
    Community Member
    9 Posts

    Re: 301 redirect problem, htaccess file and Silverstripe 2.4.5 Link to this post

    Getting some success with an old post here http://silverstripe.org/general-questions/show/15090. If I use the following code in the RewriteCond part of the htaccess file:

       RewriteCond %{HTTP_HOST} ^http://www.mydomain.co.uk/eurouser/
       RewriteRule ^(.*)$ http://www.mydomains.co.uk/news-and-events/european-user-group/ [R=permanent,L]

    I get the desired page redirect and the site navigation doesn't break:

    ### SILVERSTRIPE START ###

    <Files *.ss>
       Order deny,allow
       Deny from all
       Allow from 127.0.0.1
    </Files>

    <Files web.config>
       Order deny,allow
       Deny from all
    </Files>

    ErrorDocument 404 /assets/error-404.html
    ErrorDocument 500 /assets/error-500.html

    <IfModule mod_alias.c>
       RedirectMatch 403 /silverstripe-cache(/|$)
    </IfModule>

    <IfModule mod_rewrite.c>
       SetEnv HTTP_MOD_REWRITE On
       RewriteEngine On

       RewriteCond %{REQUEST_URI} ^(.*)$
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]

       RewriteCond %{HTTP_HOST} ^http://www.mydomain.co.uk/eurouser/
       RewriteRule ^(.*)$ http://www.mydomains.co.uk/news-and-events/european-user-group/ [R=permanent,L]
    </IfModule>
    ### SILVERSTRIPE END ###

    However, if I add another RewriteCond the second redirect produces a 404 page error instead of a redirect ie

       RewriteCond %{HTTP_HOST} ^http://www.mydomain.co.uk/eurouser/
       RewriteRule ^(.*)$ http://www.mydomains.co.uk/news-and-events/european-user-group/ [R=permanent,L]
       RewriteCond %{HTTP_HOST} ^http://www.mydomain.co.uk/news/
       RewriteRule ^(.*)$ http://www.mydomains.co.uk/news-and-events/ [R=permanent,L]
    </IfModule>
    ### SILVERSTRIPE END ###

    Any ideas welcome!

  • octopuscreative
    Avatar
    Community Member
    9 Posts

    Re: 301 redirect problem, htaccess file and Silverstripe 2.4.5 Link to this post

    Problem solved. Stay away from the .htaccess file and install the Link Mapping Module available here:

    http://silverstripe.org/link-mapping-module/

    Installs a 'Link Mapping' tab to the CMS allowing old url addresses to be redirected to new pages on your site. Happy days.

  • davede
    Avatar
    Community Member
    24 Posts

    Re: 301 redirect problem, htaccess file and Silverstripe 2.4.5 Link to this post

    Hi all,

    I had the same issue.

    I fixed it by putting some ReWrite Rules after the ReWrite Base command and before the SS commands e.g.

    <IfModule mod_rewrite.c>
       SetEnv HTTP_MOD_REWRITE On
       RewriteEngine On
       RewriteBase /

       ##my addition below
       RewriteRule ^guides/tag/(.*) http://www.example.com/page-to-redirect-to/ [R=301,L]

       
       RewriteCond %{REQUEST_URI} ^(.*)$
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
       
       
    </IfModule>
    ### SILVERSTRIPE END ###

    Don't forget to clear your browser cache if the redirect doesn't appear to be working! (CTRL+ ALT + DEL) - Thanks to Pyromanik on the IRC channel for the tip.

    3823 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.