简体   繁体   中英

Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirect

NOT A DUPLICATE TO Laravel, how to redirect as 301 and 302:

I'm using Laravel 4.1, php 5.4 on Apache webserver. I'd like to redirect all requests coming to

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

I'm calculating my canonical URL to be placed in the Head section as follows:

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

How can I do the redirect?

Sorry to have bothered you, but I found the answer now.

I added the following code in my 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());
   }
 }); 

Work like a charm, nothing else needs to be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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