iUsable
iUsable

Reputation: 361

Redirecting non www to www in CakePHP using app/webroot

Gurus of So

I am trying to redirect any user that comes to http://domain.com to http://www.domain.com. I have found a bunch of different ways to do that on SO, but I need to make them fit the dynamic app config from CakePHP.

Currently, this is what my .htaccess reads:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ app/webroot [L]
    RewriteRule (.*) app/webroot [L]
</IfModule>

I am running Ubuntu 10.04 on a Slicehost slice.

Any help is super appreciated. Thank you.

UPDATE - SOLUTION - Thanks to Rob Wilkerson

<IfModule mod_rewrite.c>
    RewriteEngine On

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

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app/webroot/$1 [QSA,L]
</IfModule>

Upvotes: 2

Views: 1865

Answers (1)

Rob Wilkerson
Rob Wilkerson

Reputation: 41236

From one of my own apps:

<IfModule mod_rewrite.c>
  RewriteEngine On

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

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Upvotes: 6

Related Questions