URL redirection using .htaccess in Cakephp 2.0

I am using Cakephp 2.0, and my website’s domain name is www.sample.com. If I try to access sample.com (without www), his visit to www.sample.com will be great. But my problem is that my domain consists of a large number of pages for example:

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

If I access the above URL, for example http://sample.com/users/login , it redirects to

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

but he needs to redirect to

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

I already wrote the following .htaccess code (in front of the application folder) to redirect

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

Inside the webroot folder, I have one .htacces file that contains the following code Perhaps this is a problem, I think

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

+4
source share
2 answers

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

 ## 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] 
+3
source

Try writing the following rule:

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

in the app / webroot / .htaccess file, and not in the .htaccess application folder. It works great for me.

0
source

Source: https://habr.com/ru/post/1444771/


All Articles