Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » htaccess - rewrite no www to www problem

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

Page: 1
Go to End
Author Topic: 3695 Views
  • DanStephenson
    Avatar
    Community Member
    115 Posts

    htaccess - rewrite no www to www problem Link to this post

    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?

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

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

    Works for me ?

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

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

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

    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 ?

  • bennylope
    Avatar
    Community Member
    12 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

    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.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

    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.

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

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

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: htaccess - rewrite no www to www problem Link to this post

    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

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