使用 URL 参数在 Laravel 中将 www 重定向到非 www [英] Redirect www to non-www in Laravel with URL parameters

查看:37
本文介绍了使用 URL 参数在 Laravel 中将 www 重定向到非 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 URL 从 www.myapp.co 重定向到 myapp.co ?此外,如果 URL 有其他参数,如 www.myapp.co/other-parameter 将被重定向到 myapp.co/other-parameter 其中 other-参数 可以是任何东西.很抱歉这个菜鸟问题,我还在使用 Laravel 1 周.

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.

顺便说一句,我使用的是动态子域,所以我只需要将 www 重定向到非 www 而不是其他子域.

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;
    });

});

推荐答案

也许你可以使用 public 文件夹中的 .htaccess 来进行重定向.

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

这是一个:

<IfModule mod_rewrite.c>
    RewriteEngine On

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

基本上在这里我在发出重定向到 www 之前测试主机是否不是本地主机而不是子域(形式 abc.xyz.tldn).我在多个项目中使用这种代码,但尚未对其进行测试.我希望它能开箱即用.

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.

此处描述了另一种无需复杂模式匹配的方法:在 htaccess 中将非 www 重定向到 wwww但是这个解决方案需要你在 htaccess 中硬编码你的域名.(可能有问题也可能没有问题,具体取决于您的设置)

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)

这篇关于使用 URL 参数在 Laravel 中将 www 重定向到非 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆