【问题标题】:Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirectLaravel 4.1 使用 301 永久重定向将所有请求从 www.mydomain.com 重定向到 mydomain.com
【发布时间】:2015-05-20 18:00:10
【问题描述】:

NOT A DUPLICATE TO Laravel,如何重定向为 301 和 302:

我在 Apache 网络服务器上使用 Laravel 4.1、php 5.4。我想将所有请求重定向到

 http://www.example.com/whateva 
 to 
 http://example.com/whateva

我正在计算要放置在 Head 部分中的规范 URL,如下所示:

$canonicalURL = Request::url() . $url_param;
$canonicalURL = str_replace("http://www.example.com","http://example.com", $canonicalURL);

如何进行重定向?

【问题讨论】:

  • 谢谢...不要认为它是重复的,这里的问题有点不同,因为我想重定向整个子域。正如我们在下面看到的,解决方案非常不同。

标签: php .htaccess redirect laravel-4


【解决方案1】:

很抱歉打扰您,但我现在找到了答案。

我在app/filter.php中添加了以下代码

App::before(function($request){
   //Remove the 'www.' from all domains
   if (substr($request->header('host'), 0, 4) === 'www.') {
      $request->headers->set('host', 'example.com');
     return Redirect::to($request->path());
   }
 }); 

像魅力一样工作,没有其他事情需要做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2014-09-24
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多