how can redirect domain non-www to www in laravel 4?

泪湿孤枕 提交于 2019-12-12 04:11:23

问题


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:


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.



来源:https://stackoverflow.com/questions/36811573/how-redirect-no-www-url-to-www-url-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!