I need to redirect for several domains from http://www. to http://. in laravel5.6 app

Published 2 months ago by Ford555

Hello,

i have 3 domains example.es , example.cat and example.com whith same code. And i need for seo quit the alias www (redirect it without www)

example http://www.example.com to http://example.com and the same with the others.

┬┐whats the best for do it in laravel5.6 application?

  1. .htaccess??? i tried something but it doesnt works 2)middleware??? how?

can someone put the code i need, thank you!

Best Answer (As Selected By Ford555)
Cronix

Ah, well that's the problem. You're using nginx, not apache, so .htaccess doesn't even get used as that's strictly a Apache server thing.

For homestead, you'd need to ssh into the vm and go to /etc/nginx/sites-available and for each of your domains (there will be a file for each one in there), you'd need to add the redirect like this. Put this block as the very first block (above your other "server" definitions.

server {
    listen 80;
    server_name www.yoursite.com;
    return 301 $scheme://yoursite.com$request_uri;
}

and make sure you have both "www.yoursite.com" and "yoursite.com" set up in your hosts file.

Be sure to restart the nginx service after making the changes to the site configs.

Cronix
Cronix
2 months ago (726,250 XP)

You'd do it in your server config, not laravel. It would be a lot faster doing it there before it even touches laravel. If using apache, .htaccess could work.

Is apache your webserver? What have you tried in .htaccess?

Typically it's just:

# must be BELOW "RewriteEngine On"
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ http://yoursite.com/ [L,R=301]
Ford555

thank for the response. I am going to use nginx in plesk (i am finishing my website). In local i have homestead. i used somehitng like this

RewriteCond %{HTTPS} on

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

RewriteRule ^(.*)$ https://%1/ [R=301,L]

In plesk i have main domain and then 3 alias for this domain. and i dont know if i can redirect to non www my alias domains from there

Cronix
Cronix
2 months ago (726,250 XP)

Ah, well that's the problem. You're using nginx, not apache, so .htaccess doesn't even get used as that's strictly a Apache server thing.

For homestead, you'd need to ssh into the vm and go to /etc/nginx/sites-available and for each of your domains (there will be a file for each one in there), you'd need to add the redirect like this. Put this block as the very first block (above your other "server" definitions.

server {
    listen 80;
    server_name www.yoursite.com;
    return 301 $scheme://yoursite.com$request_uri;
}

and make sure you have both "www.yoursite.com" and "yoursite.com" set up in your hosts file.

Be sure to restart the nginx service after making the changes to the site configs.

Ford555

ok i always used apache before (for 10 years) and now this is my first time i use nginx because i thought laravel is implemented with nginx.

For homestead your explanation is very helpfull!! i am going to implement this now, but then how i can do it in production? do you know how to do it? i use plesk

thanks a lot and sorry about my english

Cronix
Cronix
2 months ago (726,250 XP)

I couldn't really help with the Plesk aspect. I haven't use it for like 15 years lol. You said plesk is using nginx, so I'm sure there's some place in there to edit the nginx config? Typically they have some setting you can just select to redirect www to non www, but again, it's been a very long time since I've used a plesk control panel.

Ford555

Thanks! Yes plesk has a place for put text for nginx and i Think i got it. It was a pleasure! Thanks

Cronix
Cronix
2 months ago (726,250 XP)

@Ford555 You're welcome! I'm glad it's working. Please mark the thread as solved by clicking the checkbox icon under the persons avatar next to the post that helped. It helps others when searching for a similar issue.

Please sign in or create an account to participate in this conversation.