Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Migrating a Site to Silverstripe /

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

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

301 redirect problem, htaccess file and Silverstripe 2.4.5


Go to End


4 Posts   8149 Views

Avatar
octopuscreative

Community Member, 10 Posts

19 July 2011 at 5:59am

Edited: 19/07/2011 6:28am

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

Avatar
octopuscreative

Community Member, 10 Posts

19 July 2011 at 6:35am

Edited: 19/07/2011 7:29am

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!

Avatar
octopuscreative

Community Member, 10 Posts

20 July 2011 at 12:37am

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.

Avatar
davede

Community Member, 24 Posts

29 July 2011 at 7:34am

Edited: 29/07/2011 10:17pm

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.