Answers.Body: <p>It's not really <code>Craft</code> related, but assuming that you use <code>.htaccess</code>  you could set up rewrite conditions to redirect the request:</p>
  
  <pre><code># Rewrite Engine
  RewriteEngine On
  
  # force HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
  
  # force www
  RewriteCond %{HTTP_HOST} ^foo.com [NC]
  RewriteRule ^(.*)$ http://www.foo.com/$1 [L,R=301,NC]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T12:16:07.273Z
Answers.Text: Hey, yeah! You're right. It's not Craft-related. I ended up using .htaccess too. Not tested it in production yet -but Hyperlane- which is my hoster, seems to be using the .htaccess file. A little learning curve for one who's spent much of his life in the Microsoft world ;-)
Answers.UserDisplayName: nitech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T12:14:19.993Z
Answers.LastActivityDate: 2019-06-27T12:18:47.770Z
Answers.LastEditDate: 2019-06-27T12:18:47.770Z
Answers.UserDisplayName: HendrikEng
Body: <p>When users come through the root level of the domain, ie: <a href="http://foo.com" rel="nofollow noreferrer">http://foo.com</a>, I want them to be redirected to <a href="http://www.foo.com" rel="nofollow noreferrer">http://www.foo.com</a> </p>
  
  <p>How should I go about doing this?</p>
  
  <h3>Sample .htaccess-file</h3>
  
  <p>Thanks to @HendrikEng for his help and clarification. Based on what he said and what I found at <a href="https://www.hashtagerrors.com/blog/the-perfect-htaccess-file-for-craft" rel="nofollow noreferrer">https://www.hashtagerrors.com/blog/the-perfect-htaccess-file-for-craft</a> I'm using this .htaccess file. Feel free to copy.</p>
  
  <p>The reason I am redirecting to a specific domain instead of using @HendrikEng's suggestion is that I am running in a multi staging environment (at Hyperlane), and needed to avoid redirects to www. when running on their development or staging environments.</p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
      RewriteEngine On
  
      # Send would-be 404 requests to Craft
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
      RewriteRule (.+) index.php?p=$1 [QSA,L]
  
      # Add https://www to beginning of URL 
      RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
      RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]    
  
      # Mime types
      # Audio
      AddType audio / ogg .oga ogg
      AddType audio / mp4 .m4a.f4a.f4b
      # Video
      AddType video / ogg .ogv
      AddType video / mp4 .mp4.m4v.f4v.f4p
      AddType video / webm .webm
      AddType video / x - flv .flv
      # Images
      AddType image / x - icon .ico
      AddType image / webp .webp
      AddType image / gif .gif.GIF
      AddType image / jpeg .jpeg.jpg.jpe.JPG
      AddType image/svg+xml .svg .svgz
      # Fonts
      AddType application / vnd.ms - fontobject .eot
      AddType application / x - font - ttf .ttf.ttc
      AddType font / opentype .otf
      AddType application / x - font - woff .woff
      # JAVASCRIPT
      AddType application / javascript .js.jsonp
      AddType application / json json
      # Extras
      AddType text / x - component .htc
      AddType application / xml .rss.atom.xml.rdf
      AddType application / x - chrome - extension .crx
      AddType application / x - opera - extension .oex
      AddType application / x - xpinstall .xpi
      AddType application / octet - stream .safariextz
      AddType application / x - web - app - manifest + jsonp .webapp
      AddType text / x - vcard .vcf
      AddType application / x - shockwave - flash .swf
  
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_expires.c&gt;
      ExpiresActive on
      # Perhaps better to whitelist expires rules? Perhaps.
      ExpiresDefault "access plus 1 month"
      # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
      ExpiresByType text/cache-manifest       "access plus 0 seconds"
      # Your document html
      ExpiresByType text/html                 "access plus 30 minutes"
      # Data
      ExpiresByType text/xml                  "access plus 0 seconds"
      ExpiresByType application/xml           "access plus 0 seconds"
      ExpiresByType application/json          "access plus 0 seconds"
      # Feed
      ExpiresByType application/rss+xml       "access plus 1 hour"
      ExpiresByType application/atom+xml      "access plus 1 hour"
      # Favicon (cannot be renamed)
      ExpiresByType image/x-icon              "access plus 1 week"
      # Media: images, video, audio
      ExpiresByType image/gif                 "access plus 1 month"
      ExpiresByType image/png                 "access plus 1 month"
      ExpiresByType image/jpeg                "access plus 1 month"
      ExpiresByType image/svg+xml             "access plus 1 month"
      ExpiresByType video/ogg                 "access plus 1 month"
      ExpiresByType audio/ogg                 "access plus 1 month"
      ExpiresByType video/mp4                 "access plus 1 month"
      ExpiresByType video/webm                "access plus 1 month"
      # HTC files  (css3pie)
      ExpiresByType text/x-component          "access plus 1 month"
      # Webfonts
      ExpiresByType application/x-font-ttf    "access plus 1 month"
      ExpiresByType font/opentype             "access plus 1 month"
      ExpiresByType application/x-font-woff   "access plus 1 month"
      ExpiresByType image/svg+xml             "access plus 1 month"
      ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
      # CSS and JavaScript
      ExpiresByType text/css                  "access plus 1 year"
      ExpiresByType application/javascript    "access plus 1 year"
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_rewrite.c&gt;
     RewriteCond %{SCRIPT_FILENAME} -d [OR]
     RewriteCond %{SCRIPT_FILENAME} -f
     RewriteRule "(^|/)\." - [F]
  &lt;/IfModule&gt;
  
  &lt;FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$"&gt;
     Order allow,deny
     Deny from all
     Satisfy All
  &lt;/FilesMatch&gt;
  
  &lt;IfModule php5_module&gt;
      php_value session.cookie_httponly true
  &lt;/IfModule&gt;
  </code></pre>
  
Tags: <multi-domain>
Title: How should I set up redirects from http://foo.com to http://www.foo.com
ThreadText: # How should I set up redirects from http://foo.com to http://www.foo.com
  
  (Asked by: username_0 on 2019-06-27)
  
  When users come through the root level of the domain, ie: <http://foo.com>, I want them to be redirected to <http://www.foo.com>
  
  How should I go about doing this?
  
  ### Sample .htaccess-file
  
  Thanks to @username_1 for his help and clarification. Based on what he said and what I found at <https://www.hashtagerrors.com/blog/the-perfect-htaccess-file-for-craft> I'm using this .htaccess file. Feel free to copy.
  
  The reason I am redirecting to a specific domain instead of using @username_1's suggestion is that I am running in a multi staging environment (at Hyperlane), and needed to avoid redirects to www. when running on their development or staging environments.
  
  ```
  <IfModule mod_rewrite.c>
      RewriteEngine On
  
      # Send would-be 404 requests to Craft
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
      RewriteRule (.+) index.php?p=$1 [QSA,L]
  
      # Add https://www to beginning of URL 
      RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
      RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]    
  
      # Mime types
      # Audio
      AddType audio / ogg .oga ogg
      AddType audio / mp4 .m4a.f4a.f4b
      # Video
      AddType video / ogg .ogv
      AddType video / mp4 .mp4.m4v.f4v.f4p
      AddType video / webm .webm
      AddType video / x - flv .flv
      # Images
      AddType image / x - icon .ico
      AddType image / webp .webp
      AddType image / gif .gif.GIF
      AddType image / jpeg .jpeg.jpg.jpe.JPG
      AddType image/svg+xml .svg .svgz
      # Fonts
      AddType application / vnd.ms - fontobject .eot
      AddType application / x - font - ttf .ttf.ttc
      AddType font / opentype .otf
      AddType application / x - font - woff .woff
      # JAVASCRIPT
      AddType application / javascript .js.jsonp
      AddType application / json json
      # Extras
      AddType text / x - component .htc
      AddType application / xml .rss.atom.xml.rdf
      AddType application / x - chrome - extension .crx
      AddType application / x - opera - extension .oex
      AddType application / x - xpinstall .xpi
      AddType application / octet - stream .safariextz
      AddType application / x - web - app - manifest + jsonp .webapp
      AddType text / x - vcard .vcf
      AddType application / x - shockwave - flash .swf
  
  </IfModule>
  
  <IfModule mod_expires.c>
      ExpiresActive on
      # Perhaps better to whitelist expires rules? Perhaps.
      ExpiresDefault "access plus 1 month"
      # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
      ExpiresByType text/cache-manifest       "access plus 0 seconds"
      # Your document html
      ExpiresByType text/html                 "access plus 30 minutes"
      # Data
      ExpiresByType text/xml                  "access plus 0 seconds"
      ExpiresByType application/xml           "access plus 0 seconds"
      ExpiresByType application/json          "access plus 0 seconds"
      # Feed
      ExpiresByType application/rss+xml       "access plus 1 hour"
      ExpiresByType application/atom+xml      "access plus 1 hour"
      # Favicon (cannot be renamed)
      ExpiresByType image/x-icon              "access plus 1 week"
      # Media: images, video, audio
      ExpiresByType image/gif                 "access plus 1 month"
      ExpiresByType image/png                 "access plus 1 month"
      ExpiresByType image/jpeg                "access plus 1 month"
      ExpiresByType image/svg+xml             "access plus 1 month"
      ExpiresByType video/ogg                 "access plus 1 month"
      ExpiresByType audio/ogg                 "access plus 1 month"
      ExpiresByType video/mp4                 "access plus 1 month"
      ExpiresByType video/webm                "access plus 1 month"
      # HTC files  (css3pie)
      ExpiresByType text/x-component          "access plus 1 month"
      # Webfonts
      ExpiresByType application/x-font-ttf    "access plus 1 month"
      ExpiresByType font/opentype             "access plus 1 month"
      ExpiresByType application/x-font-woff   "access plus 1 month"
      ExpiresByType image/svg+xml             "access plus 1 month"
      ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
      # CSS and JavaScript
      ExpiresByType text/css                  "access plus 1 year"
      ExpiresByType application/javascript    "access plus 1 year"
  </IfModule>
  
  <IfModule mod_rewrite.c>
     RewriteCond %{SCRIPT_FILENAME} -d [OR]
     RewriteCond %{SCRIPT_FILENAME} -f
     RewriteRule "(^|/)\." - [F]
  </IfModule>
  
  <FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$">
     Order allow,deny
     Deny from all
     Satisfy All
  </FilesMatch>
  
  <IfModule php5_module>
      php_value session.cookie_httponly true
  </IfModule>
  
  ```
  
  **Tags:** multi-domain
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-27. Score: 2 | Accepted answer
  
  It's not really `Craft` related, but assuming that you use `.htaccess` you could set up rewrite conditions to redirect the request:
  
  ```
  # Rewrite Engine
  RewriteEngine On
  
  # force HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
  
  # force www
  RewriteCond %{HTTP_HOST} ^foo.com [NC]
  RewriteRule ^(.*)$ http://www.foo.com/$1 [L,R=301,NC]
  
  ```
  
  > **Comments:**
  > **username_0**: Hey, yeah! You're right. It's not Craft-related. I ended up using .htaccess too. Not tested it in production yet -but Hyperlane- which is my hoster, seems to be using the .htaccess file. A little learning curve for one who's spent much of his life in the Microsoft world ;-)  
  
  ---
  
  