简体   繁体   English

CakePHP 3 中的 301 重定向

[英]301 Redirects in CakePHP 3

I'm new to CakePHP.我是 CakePHP 的新手。 I have a website that was built by someone else in CakePHP 3.3.16 .我有一个由别人在CakePHP 3.3.16 中构建的网站。 There are links to website.com/page and also website.com/page/ .有指向website.com/pagewebsite.com/page/ 的链接。

What's the best way to get /page/ redirecting to /page without affecting anything else that might start with a /page/ ?/page/重定向到/page而不影响其他任何可能以/page/开头的内容的最佳方法是什么?

The route.php has this... route.php有这个...

$routes->connect('/page/*', ['controller' => 'Page', 'action' => 'something']);

Then I have a PageController.php which has然后我有一个PageController.php

public function something($site_id = null)
{
...
}

Will this work in the routes.php ?这会在routes.php 中工作吗? How would I specify that this is a 301 redirect?我如何指定这是 301 重定向?

use Cake\Routing\RouteBuilder;
Router::scope('/', function (RouteBuilder $routes) {
      $routes->redirect('/page/','http://website.com/page');
      $routes->connect('/page/?*', ['controller' => 'Page', 'action' => 'something']);
   });

This doesn't seem to work in the .htaccess (/page/ is displayed and not redirected)...这在.htaccess 中似乎不起作用(/page/ 显示但未重定向)...

Redirect 301 /page/ http://website.com/page
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

as a first glimpse to fix this would be instead of putting the Route::scope would be Router::connect and Router::redirect imo.作为解决此问题的第一眼,不是将Route::scopeRouter::connectRouter::redirect imo。 Therefore, an approach to a solution would be first doing something like this.因此,解决方案的方法将首先做这样的事情。

 Router::connect('/page/*', ['controller' => 'Page', 'action' => 'something']);

And then you redirect the page with the cake command redirect:然后使用 cake 命令重定向重定向页面:

 Router::redirect('/page/','/page', array('status' => 301));

In the project that I use which is CakePhp 2.6, I always have redirected pages like this depending on the task.在我使用的 CakePhp 2.6 项目中,我总是根据任务重定向这样的页面。 Sometimes you can do this type of redirects inside the Controller but it is best to avoid it for not mixing routing with programming logic.有时您可以在控制器内部执行这种类型的重定向,但最好避免它,因为不要将路由与编程逻辑混合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM