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.

Customising the CMS

htaccess - rewrite no www to www problem


Reply

8 Posts   3786 Views

Avatar
DanStephenson

15 May 2009 at 6:02am Community Member, 115 Posts

Hi all,

I am adding this to my .htaccess:

RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

So that I can redirect all "non-www" users to "www" users. However, when I do this, I get redirected to www.mysite.com/sapphire/main.php?url=/ and then I get a 404 error.

How can I get this to work correctly with the framework?

Avatar
Fuzz10

15 May 2009 at 7:26am Community Member, 787 Posts

############ Redirect for Google
RewriteCond %{HTTP_HOST} !^www.lemon8.nl$ [NC]
RewriteRule ^(.*)$ http://www.lemon8.nl/$1 [L,R=301]
############

Works for me ?

Avatar
Howard

17 May 2009 at 10:42pm Community Member, 215 Posts

Just put Director::forceWWW() in mysite/ _config.php :)

Avatar
DanStephenson

18 September 2009 at 4:01am (Last edited: 18 September 2009 4:11am), Community Member, 115 Posts

If I put Director::forceWWW(); into my _config file, the users going to the non WWW version are redirected to http://www.example.com/sapphire/main.php?url=/

How can I get them to just be redirected to http://www.example.com ?

Avatar
bennylope

4 February 2010 at 3:59pm Community Member, 12 Posts

I had the same problem. Here's what my code looked like:

RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC]

Here's what worked:

RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC,L]

I tried Dan's flags and it did work for me, so not sure what was going on there. The Director forceWWW method is more elegant, but I'd rather have this processed before it hits the CMS.

Avatar
Hamish

5 February 2010 at 8:10am Community Member, 712 Posts

Dan, are you adding the rules before or after the SilverStripe rewriting rules? The order is important - it would be before, but I think you have them after.

Avatar
Ben_W

19 February 2010 at 6:50pm Community Member, 80 Posts

Yes, the order of this redirect is important. the following is what I have used on my site.

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

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^mydomain\.com\.au$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com.au/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !(\.gif$)|(\.jpg$)|(\.png$)|(\.css$)|(\.js$)

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

Avatar
marc79

14 June 2011 at 3:53am Community Member, 65 Posts

Hi.

I am trying to do a similar thing with a .co.uk to .com address but with no luck.

I have added this to my .htaccess file but the site still redirects to /sapphire/main.php

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   
   RewriteCond %{HTTP_HOST} ^www\.mysite\.co\.uk$ [NC]
   RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
   
   RewriteCond %{REQUEST_URI} !(\.gif$)|(\.jpg$)|(\.png$)|(\.css$)|(\.js$)
   
   RewriteCond %{REQUEST_URI} ^(.*)$
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
</IfModule>

What am I doing wrong?

Thanks

Marc