问题
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;
});
});
回答1:
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)
回答2:
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.'.
回答3:
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]
回答4:
Add the following code to your .htaccess
file:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
回答5:
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
- Go to file zila
- Select your website
- Open .htaccess file
Copy following code to .htaccess file
RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Replace example with your domain name
- Paste it to .htaccess file
- Check it live
来源:https://stackoverflow.com/questions/26754119/redirect-www-to-non-www-in-laravel-with-url-parameters