AnNaMaLaI
AnNaMaLaI

Reputation: 4084

URL redirecting using .htaccess in Cakephp 2.0

I am using Cakephp 2.0 and my website domain name is www.sample.com , If I try to access sample.com (with out www) then its going to www.sample.com this is fine. But My problem is my domain consists of lot of pages for example :

> http://www.sample.com/users/login
> http://www.sample.com/users/add

If I access the above url like http://sample.com/users/login then it redirect to

>  http://www.sample.com/index.php?url=users/login

but it needs to redirect to

   http://www.sample.com/users/login

I already written the following code .htaccess file(before the app folder) to redirect

Rewritecond %{http_host} ^sample.com [NC]
Rewriterule ^(.*)$ http://www.sample.com/$1 [R=301,NC]

Inside webroot folder I have one .htacces file that contain the following code may be that is the problem I think

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

Upvotes: 4

Views: 2615

Answers (2)

472084
472084

Reputation: 17886

Are you trying to always remove www or always keep www? This will add it:

## Add www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

## CakePHP
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]

Upvotes: 3

Pedro
Pedro

Reputation: 691

Try to write the next rule:

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

on app/webroot/.htaccess file, not on app's folder .htaccess. It works perfectly for me.

Upvotes: 0

Related Questions