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

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

问题描述

如何将URL从www.myapp.co重定向到myapp.co?另外,如果URL具有其他参数,例如www.myapp.co/other-parameter,则会将其重定向到myapp.co/other-parameter,其中other-parameter可以是任何值.很抱歉这个菜鸟问题,我还需要1周的时间才能使用Laravel.

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参数将www重定向到Laravel中的非www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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