1

@Starkeen, this code didn't work for me. All other pages now have 404 errors and homepage didn't get the www added.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.com$
RewriteRule ^(.*)$ http://www.sitename.com/$1 [NC,R=301,L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [R=301,L]
</IfModule>

help please

  • Possible duplicate of: http://stackoverflow.com/questions/9314520/htaccess-get-all-non-www-traffic-redirected-to-the-www – user2182349 Jul 26 '15 at 14:42
  • @Starkeen, this is what I have. It seems a bit long right? –  Jul 26 '15 at 14:55
  • @Starkeen saved it but the www didn't get added and all other pages get the 404 error, help buddy! many thanks –  Jul 26 '15 at 15:10

1 Answers1

0

add this to your htaccess file right after the "RewriteEngine on" directive

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

Edit

Added the missing L flag to first Rewrite

Try this :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [NC,R=301,L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [NC,R=301,L]
</IfModule>
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thanks @Starkeen, do I need to have a space between your code and that directive? Should I remove the RewriteCond/RewriteRule from the bottom as well? –  Jul 26 '15 at 14:47
  • the homepage works without www added and all other pages have gone offline –  Jul 26 '15 at 15:17
  • Zwan , sorry there was a typo, I edited my answer, Reload the page to see it. – Amit Verma Jul 26 '15 at 16:09
  • homepage is online but still no www and all other pages are getting the 404 error. Here is a link to the site: http://bit.ly/1CZqrN2 –  Jul 26 '15 at 16:15
  • Clear your browser's cache and try – Amit Verma Jul 26 '15 at 16:24
  • cleared cache and still not working, please see site. –  Jul 26 '15 at 16:30
  • Frank, the code is working fine on my apache server, non www to www is redirecting successfully, and path that is not related to existing files and dir is redirecting to **index.php** . – Amit Verma Jul 26 '15 at 16:41
  • why is not working on my server? All other pages are 404 errors –  Jul 26 '15 at 16:44
  • is htaccess enabled on your server? – Amit Verma Jul 26 '15 at 16:48
  • No problem, glad to hear it worked, you may accept this answer if it worked. – Amit Verma Jul 26 '15 at 18:08