how can redirect domain non-www to www in laravel

2019-03-03 19:57发布

Hello I try to redirect my domain name from non-www to www via .htaccess file. My previous htaccess code was

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

I replace it by the code

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.forexfunction\.com
RewriteRule (.*) http://www.forexfunction.com/$1 [R=301,L]

but i face 404 errors after replacing this code. How can i solve my problem? Thanks in advance.

1条回答
爷的心禁止访问
2楼-- · 2019-03-03 20:12

This is the code which will redirect non-www request to www request.

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Here is how you .htacccess file will look like.

<IfModule mod_rewrite.c>
  Options -MultiViews
  RewriteEngine On

  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [L]

</IfModule>

Hope it helps.

查看更多
登录 后发表回答