8

How can I redirect URL from www.myapp.co to myapp.co ? And also if the URL has other parameter like www.myapp.co/other-parameter will be redirected to myapp.co/other-parameter where the other-parameter can be anything. Sorry for this noob question, I am still 1 week in using Laravel.

BTW, I am using dynamic subdomain so I need only the redirect www to non-www and not the other subdomains.

Route::group(array('domain' => 'myapp.co'), function() {

    Route::get('/', function() {
        return 'Hello! Welcome to main page...';
    });
    Route::get('/login', function() {
        return 'Login';
    });
    Route::get('/signup', function() {
        return 'Signup';
    });

});

Route::group(array('domain' => '{account}.myapp.co'), function() {

    Route::get('/', function($account) {

        if ($account == 'www') {
            return Redirect::to('http://myapp.co');
        }
        return $account;
    });

});
Port 8080
  • 858
  • 3
  • 12
  • 24
  • possible duplicate of [Redirect non-www to www in .htaccess](http://stackoverflow.com/questions/12050590/redirect-non-www-to-www-in-htaccess) – Bogdan Nov 05 '14 at 09:59
  • @Bogdan The one which you have mentioned is the opposite of what OP asked. It's not a duplicate. Its the opposite. – Anbuselvan Rocky Sep 28 '22 at 20:10

6 Answers6

17

If you really want to treat your problem programmatically I think the best solution is to add the next code to your filters.php:

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

Basically I am just creating a filter to check if the request starts with 'www.' and in this case redirecting the client to the same path without the 'www.'.

  • I had to do the redirect inside the code, and this was perfect! – Patrick Dec 17 '14 at 02:33
  • Just if anyone facing this issue, when I used above code then I faced "Class App\Http\Controllers\Redirect not found" issue that I solved mentioning "use Redirect" at top – Jass Nov 04 '18 at 09:52
12

Maybe you can use a .htaccess inside thepublic folder to do the redirection.

Here is one:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !localhost
    RewriteCond %{HTTP_HOST} !^(.+)\.(.+)\.(.+)
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
</IfModule>

Basicaly here I'm testing if the host is not localhost and not a subdomain (form abc.xyz.tldn) before issuing a redirect to www. I'm using this sort of code in multiple projects but havn't tested it. I hope it will work out of the box.

Edit: Another way to do it without complex pattern matching is described here: Redirect non www to wwww in htaccess But this solution require you to hardcode your domain name inside the htaccess. (may or may not be a problem depending on your setup)

Community
  • 1
  • 1
Atrakeur
  • 4,126
  • 3
  • 17
  • 22
  • Thanks, it works. But I have a problem. When I try `test1.myapp.co` I was redirected to `www.test1.myapp.co` which I don't want. I only want it to become `test1.myapp.co`. – Port 8080 Nov 05 '14 at 10:12
  • BTW, it works on firefox. Maybe I need to delete cache in chrome. – Port 8080 Nov 05 '14 at 10:15
7

Add the following code to your .htaccess file:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
ata
  • 3,398
  • 5
  • 20
  • 31
Tora
  • 121
  • 2
  • 8
2

Try this from http://stackoverflow.com/a/4958847/1078583 If this didn't work it is because you are using nginx

# Force WWW:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
0

.htaccess

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^www.url\.com$ [NC]
 RewriteRule ^(.*)$ https://url.com/$1 [R=301,L]
xpredo
  • 1,282
  • 17
  • 27
-3

Redirect www to non www urls

Today we learn how to redirect www urls to non www urls

Now this website opening for both www and non www

Steps to redirect from www to non www urls using .htaccess file

  1. Go to file zila
  2. Select your website
  3. Open .htaccess file
  4. Copy following code to .htaccess file

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
    
  5. Replace example with your domain name

  6. Paste it to .htaccess file
  7. Check it live
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343