user1319182
user1319182

Reputation: 496

Updating .htaccess to redirect to "www"

I have a website which uses a PHP framework (CodeIgniter) and I'm trying to redirect all the queries sent to "mydomain.com" to www.mydomain.com". I made changes to the .htaccess, which now looks as follows:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # I just added this part
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

  # The default rule of the framework
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

Now the problem is that it works fine when I enter "mydomain.com", the user is redirected properly, but they are also redirected to this home page when they enter a more complex address like "mydomain.com/Settings".

How can I address this?

Thanks

Upvotes: 0

Views: 43

Answers (3)

Arzoo
Arzoo

Reputation: 106

Add this after RewriteEngine On

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L] 

Upvotes: 0

Brian Gottier
Brian Gottier

Reputation: 4582

This works for me (example.com)

<IfModule mod_rewrite.c>
  RewriteEngine On

  # I just added this part
  RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

  # The default rule of the framework
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

For me, the "framework" part of this can vary from server to server, and in my own experience I have these options:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# NORMAL SERVER ( COMMENT OUT IF GODADDY OR OPTION #3 )
RewriteRule .* index.php/$0 [PT,L]

# SPECIAL GODADDY LINE ( UNCOMMENT IF GODADDY )
#RewriteRule ^(.*)$ /index.php?/$1 [L]

# IF NOTHING ELSE WORKS UNCOMMENT THIS ONE
#RewriteRule ^ index.php [QSA,L]

I know you were not asking about the "framework" part, but I thought I'd mention it anyway.

Upvotes: 1

jonmrich
jonmrich

Reputation: 4323

Give this a shot.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Upvotes: 0

Related Questions