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

Avatar
DanStephenson

Community Member, 115 Posts

15 May 2009 at 6:02am

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

Community Member, 787 Posts

15 May 2009 at 7:26am

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

Works for me ?

Avatar
Howard

Community Member, 215 Posts

17 May 2009 at 10:42pm

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

Avatar
DanStephenson

Community Member, 115 Posts

18 September 2009 at 4:01am

Edited: 18/09/2009 4:11am

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

Community Member, 12 Posts

4 February 2010 at 3:59pm

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

Community Member, 712 Posts

5 February 2010 at 8:10am

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

Community Member, 80 Posts

19 February 2010 at 6:50pm

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

Community Member, 65 Posts

14 June 2011 at 3:53am

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