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


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.


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.


  • htaccess - IP canonicalization to HTTPS
  • Redirect (301) from domain.com to www.domain.com, while also forcing HTTPS in HTACCESS
  • https request on some pages but not on all pages zend framework
  • Why is Google indexing Friendly URL mixed with hyphens and %20?
  • what is the best way to prevent certain directories of a web site from being directly accessed?
  • Apache ssl redirect using mod_rewrite
  • Using .htaccess to redirect http://, http://www., and https://www. to https://
  • alternative of else operator and not equal operator in nginx
  • IIS Redirecting non-WWW to WWW along with HTTP to HTTPS ISSUE
  • Redirect from few url's (both www and non-www) to original one
  • Allow from env with mod_rewrite not mod_setenvif in .htaccess
  • Add rewrite rule
  • Remove two subdirectories from url with mod_rewrite
  • SilverStripe. How to redirect from www.domainName to domainName?
  • Redirect only with specific domain
  • Nginx custom PHP error_pages with different header status codes and body contents
  • How can I .htaccess 301 redirect all pages/files (except for one) to new domain?
  • CodeIgniter and htaccess - Protect a specific controller and its methods via AuthType?
  • url rewrite not working .htaccess
  • Strip down any parameter from URL which is not whitelisted
  • Use htaccess to mask domain and folder name
  • Too many Redirects after switching WordPress to https
  • Can I used mod_rewrite to change file extensions? .jpeg to .jpg for example
  • .htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact
  • Smart URL with optional parameters?
  • htaccess settings for angularJS pushstate
  • Removing the .php file extension from the URL Request
  • Redirect to trailng slash (htaccess)
  • htaccess add www if not subdomain, if subdomain remove www
  • htaccess rewriting URLs with multiple forward slashes
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?