如何在laravel 4中将域名非www重定向到www?

时间:2014-10-24 14:02:33

标签: .htaccess laravel-4

您好我尝试通过.htaccess文件将我的域名从非www重定向到www。 我以前的htaccess代码是

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

我用代码

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

但是在替换此代码后我面临404错误。 我怎样才能解决我的问题? 提前谢谢。

1 个答案:

答案 0 :(得分:4)

这是将非www请求重定向到www请求的代码。

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

以下是.htacccess文件的外观。

<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>

希望它有所帮助。